Swift3 Swift 3:如何将属性应用于字符串的各个部分

Swift3 Swift 3:如何将属性应用于字符串的各个部分,swift3,nsattributedstring,nsrange,Swift3,Nsattributedstring,Nsrange,因此,我在Swift 2中使用的方法不再有效,因为Swift 3在字符串索引和范围方面发生了变化。以前我有 func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) { if let index = self.text?.characters.indexOf(Character("|")) { self.text = self.text!.stringByR

因此,我在Swift 2中使用的方法不再有效,因为Swift 3在字符串索引和范围方面发生了变化。以前我有

func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) {
    if let index = self.text?.characters.indexOf(Character("|")) {
        self.text = self.text!.stringByReplacingOccurrencesOfString("|", withString: "")
        let labelLength:Int = Int(String(index))! // Now returns nil

        var keyAttr: [String:AnyObject] = [NSForegroundColorAttributeName: highlightColor]
        var valAttr: [String:AnyObject] = [NSForegroundColorAttributeName: defaultColor]
        if boldKeyText {
            keyAttr[NSFontAttributeName] = UIFont.systemFontOfSize(self.font.pointSize)
            valAttr[NSFontAttributeName] = UIFont.systemFontOfSize(self.font.pointSize, weight: UIFontWeightHeavy)
        }

        let attributeString = NSMutableAttributedString(string: self.text!)
        attributeString.addAttributes(keyAttr, range: NSRange(location: 0, length: (self.text?.characters.count)!))
        attributeString.addAttributes(valAttr, range: NSRange(location: 0, length: labelLength))

        self.attributedText = attributeString

    }
}

基本上,我可以使用像“First Name:| Gary Oak”这样的字符串,让|字符前后的所有部分使用不同的颜色,或者将部分字符加粗,但我上面评论的行不再返回值,这会破坏后面的所有内容。关于如何做到这一点,您有什么想法吗?

在Swift 3中,您可以使用以下内容:

func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) {      

    if let index = self.text?.characters.index(of: Character("|")) {
        self.text = self.text!.replacingOccurrences(of: "|", with: "")
        let position = text.distance(from: text.startIndex, to: index)

        let labelLength:Int = Int(String(describing: position))!

        var keyAttr: [String:AnyObject] = [NSForegroundColorAttributeName: defaultColor]
        var valAttr: [String:AnyObject] = [NSForegroundColorAttributeName: highlightColor]
        if boldKeyText {
            keyAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize)
            valAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize, weight: UIFontWeightHeavy)
        }

        let attributeString = NSMutableAttributedString(string: self.text!)
        attributeString.addAttributes(keyAttr, range: NSRange(location: 0, length: (self.text?.characters.count)!))
        attributeString.addAttributes(valAttr, range: NSRange(location: 0, length: labelLength))

        self.attributedText = attributeString

    }

}

使用
let position=text.distance(from:text.startIndex,to:index)
得到的不是字符串位置的整数表示,而是字符串索引值。使用
text.distance(从:text.startIndex,到:index)
您可以在Swift 3中找到字符串索引的int位置

func configureLabel(defaultColor: UIColor, highlightColor: UIColor, boldKeyText: Bool) {      

    if let index = self.text?.characters.index(of: Character("|")) {
        self.text = self.text!.replacingOccurrences(of: "|", with: "")
        let position = text.distance(from: text.startIndex, to: index)

        let labelLength:Int = Int(String(describing: position))!

        var keyAttr: [String:AnyObject] = [NSForegroundColorAttributeName: defaultColor]
        var valAttr: [String:AnyObject] = [NSForegroundColorAttributeName: highlightColor]
        if boldKeyText {
            keyAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize)
            valAttr[NSFontAttributeName] = UIFont.systemFont(ofSize: self.font.pointSize, weight: UIFontWeightHeavy)
        }

        let attributeString = NSMutableAttributedString(string: self.text!)
        attributeString.addAttributes(keyAttr, range: NSRange(location: 0, length: (self.text?.characters.count)!))
        attributeString.addAttributes(valAttr, range: NSRange(location: 0, length: labelLength))

        self.attributedText = attributeString

    }

}
使用
let position=text.distance(from:text.startIndex,to:index)
得到的不是字符串位置的整数表示,而是字符串索引值。使用
text.distance(从:text.startIndex到:index)
可以找到字符串索引的int位置