【发布时间】:2015-06-24 18:01:03
【问题描述】:
我无法突出显示子字符串。这是我所做的(是“重复”问题):
这是我需要将其转换为 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)
另一个更新 所以我更新代码的地方仍然没有为子字符串着色 我正在做一个 twitter 应用程序,它用颜色标签、url 和用户屏幕名称突出显示
【问题讨论】:
-
“突出显示”对您意味着什么?简而言之,您想完成什么?您是否要制作一个标签,其中一个单词是红色的?
-
是的,我想用不同的颜色为主题标签或用户或网址着色。我所做的没有突出显示
标签: ios uitableview swift twitter