gpt4 book ai didi

ios - 突出显示 UILabel 中的子字符串

转载 作者:可可西里 更新时间:2023-10-31 23:56:34 26 4
gpt4 key购买 nike

我无法突出显示子字符串。这是我所做的(是一个“重复”问题):

这是我需要将其转换为 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)

另一个更新所以我更新代码的地方仍然没有为子字符串着色我正在做一个推特应用程序,它用颜色标签、网址和用户屏幕名称突出显示

最佳答案

我不太明白您要做什么,但这里有一个供您使用的模型:

let s = "Eat @my shorts" as NSString
var att = NSMutableAttributedString(string: s as String)
let r = s.rangeOfString("@\\w.*?\\b", options: .RegularExpressionSearch, range: NSMakeRange(0,s.length))
if r.length > 0 {
att.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: r)
}

这给出了一个属性字符串“Eat @my shorts”,其中单词“@my”是红色的。

enter image description here

希望能提供线索...

关于ios - 突出显示 UILabel 中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29706756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com