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

时间:2014-07-29 09:27:42

标签: unicode swift

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

对于可变字节长度的UNICODE字符串,NSMakeRange似乎期望以字节为单位的字符串。

有什么方法可以解决这个问题?提前谢谢。

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))

更新示例:

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)

如何从swift Range构建NSRange?

4 个答案:

答案 0 :(得分:2)

我为String创建了一个小扩展程序,适用于基于IndexInt的范围。

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)
    }
}

注意:该函数在String类上,因为我们需要字符串UTF16View在支持Unicode的Range<Index>和非Unicode感知的NSRange之间进行转换

答案 1 :(得分:1)

我希望以下功能可以帮助你

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)
        })
}

答案 2 :(得分:0)

您可以通过

将swift String转换为Swift NSString
var strAsNSString = "வாங்க வாங்க வணக்கமுங்க" as NSString

然后你可以创建NSMutableAttributedString。

答案 3 :(得分:0)

这样的事可能有用:

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

即使需要做更多的工作来确定前进的程度(如果你只想找到一个已知单词,你可以直接用NSRangeFromString获得它)