将范围<string.index>转换为NSRange </string.index>

时间:2015-04-17 10:43:11

标签: ios swift nsrange

以下是我需要将其投射到 NSRange

的地方
for user in tweet.userMentions
{          
    let userName = user.keyword
    if let startIndex = tweetTextLabel.text?.rangeOfString("@")?.startIndex
    {
        let range = startIndex...userName.endIndex
        var atrString = NSMutableAttributedString(string:tweetTextLabel.text!)
        atrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range as? NSRange)
        tweetTextLabel.attributedText = atrString
    }
}

我该怎么办?也许那里有另一个快速的功能 我正在使用swift 1.1,iOS 8.1 SDK

更新 仍然没有高调文本

 for user in tweet.userMentions{

                let text = tweetTextLabel.text!
                let nsText = text as NSString
                let textRange = NSMakeRange(0, nsText.length)
                let attributedString = NSMutableAttributedString(string: nsText)

                nsText.enumerateSubstringsInRange(textRange, options: NSStringEnumerationOptions.ByWords, { (substring, substringRange, enclosingRange, stop) -> () in

                    if (substring == user.keyword) {
                        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: substringRange)
                        println(substring)
                    }
                })
                tweetTextLabel.attributedText = attributedString
                println(attributedString)

0 个答案:

没有答案
相关问题