Allocating range <String.Index> for NSRange
This is where I need to give it to 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
}
}
What can I do? Maybe this is another feature that for quick I am using swift 1.1, iOS 8.1 SDK
Update Still not highlighting text
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)
+3
source to share
No one has answered this question yet
See similar questions:
or similar: