简体   繁体   English

如何在Swift中向Unicode字符串添加属性?

[英]How do you add an attribute to an Unicode string in Swift?

How do you add attribute to a UNICODE string in Swift? 如何在Swift中向UNICODE字符串添加属性?

NSMakeRange seems to expect String in bytes for a variable byte length UNICODE string. 对于可变字节长度的UNICODE字符串,NSMakeRange似乎期望String以字节为单位。

Any way to solve this? 有办法解决这个问题吗? Thanks in advance. 提前致谢。

var s:NSMutableAttributedString = NSMutableAttributedString(string: "வாங்க வாங்க வணக்கமுங்க")
s.string[0...2]
s.addAttribute(NSForegroundColorAttributeName, value:UIColor(red:0.0, green:1.0, blue:0.0, alpha:1.0), range:NSMakeRange(0,3))

UPDATED EXAMPLE: 更新的例子:

let text = "வாங்க வாங்க வணக்கமுங்க"
var startOfWord = advance(text.startIndex, 4)
var endOfWord = advance(startOfWord, 3)
var highlightRange = startOfWord..<endOfWord
text[startOfWord..<endOfWord]

let attrText = NSMutableAttributedString(string:text)
attrText.addAttribute(NSForegroundColorAttributeName, value:UIColor(red:0.0, green:1.0, blue:0.0, alpha:1.0), range:highlightRange)

How do construct NSRange from swift Range? 如何从swift Range构建NSRange?

I created a small extension for String which works for both Index based and Int based Ranges. 我为String创建了一个小扩展,它适用于基于Index和基于Int的范围。

extension String {
    func NSRangeFromRange(swiftRange: Range<Index>) -> NSRange {
        let start = swiftRange.startIndex.samePositionIn(utf16)
        let end = swiftRange.endIndex.samePositionIn(utf16)
        return NSRange(location: utf16.startIndex.distanceTo(start), length: start.distanceTo(end))
    }
    func NSRangeFromRange(swiftRange: Range<Int>) -> NSRange {
        let indexRange = Range(start: startIndex.advancedBy(swiftRange.startIndex), end: startIndex.advancedBy(swiftRange.endIndex))
        return NSRangeFromRange(indexRange)
    }
}

Note: The function is on the String class because we need the strings UTF16View to convert between the Unicode-aware Range<Index> and the non-Unicode-aware NSRange . 注意:该函数在String类上,因为我们需要字符串UTF16View在Unicode感知Range<Index>和非Unicode感知NSRange之间进行转换。

I hope the below function may help you 我希望以下功能可以帮助你

func findAndAddAttributeString(str: String, query : String) {

    let strASNSString = str as NSString

    println("employeeIdAsNSString, \(strASNSString)")

    var attributeDictionary = NSMutableDictionary(objects: [UIColor.grayColor(), UIFont.systemFontOfSize(17)], forKeys: [NSForegroundColorAttributeName, NSFontAttributeName])


    println("attributeDictionary, \(attributeDictionary)")

    var attributedEmployeeId = NSMutableAttributedString(string: strASNSString, attributes: attributeDictionary)

    println("attributedEmployeeId = \(attributedEmployeeId)")

    var error:NSError?

    var regex = NSRegularExpression.regularExpressionWithPattern(query, options: NSRegularExpressionOptions.CaseInsensitive, error: &error)
    println("regex = \(regex)")

    var range = NSMakeRange(0, strASNSString.length)
    println("range = \(range)")

    regex.enumerateMatchesInString(strASNSString, options: nil, range: range, usingBlock:{ (textCheckingResult, MatchingFlags, unsafePointer) in
        println("textCheckingResult \(textCheckingResult.rangeAtIndex(0))")
        var subStringRange = textCheckingResult.rangeAtIndex(0)
        attributedEmployeeId.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: subStringRange)
        })
}

You can convert a swift String to Swift NSString by 您可以将swift String转换为Swift NSString

var strAsNSString = "வாங்க வாங்க வணக்கமுங்க" as NSString

And then you can create NSMutableAttributedString. 然后你可以创建NSMutableAttributedString。

Something like this might work: 像这样的东西可能会起作用:

let text = "வாங்க வாங்க வணக்கமுங்க"
var startOfWord = advance(text.startIndex, 5)
var endOfWord = advance(startOfWord, 5)
var range = startOfWord..<endOfWord

Even if some more work is required to figure out how far to advance (if you are looking for just one known word you can directly get it with NSRangeFromString) 即使需要做更多的工作来确定前进的程度(如果你只想找到一个已知单词,你可以直接用NSRangeFromString获得它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM