突出显示UILabel中的子字符串

时间:2015-04-17 18:25:17

标签: ios uitableview swift twitter

我可以;突出显示一个子串。这就是我所做的(是一个"重复"问题):

以下是我需要将其投射到 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)

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

4 个答案:

答案 0 :(得分:10)

我不太明白你要做什么,但这里有一个模特供你工作:

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

希望提供线索......

答案 1 :(得分:1)

迅速找到此扩展名:

extension UILabel {

    func highlight(searchedText: String?, color: UIColor = .red) {
        guard let txtLabel = self.text?.lowercased(), let searchedText = searchedText?.lowercased() else {
            return
        }

        let attributeTxt = NSMutableAttributedString(string: txtLabel)
        let range: NSRange = attributeTxt.mutableString.range(of: searchedText, options: .caseInsensitive)

        attributeTxt.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)

        self.attributedText = attributeTxt
    }

}

答案 2 :(得分:1)

Swift5 版本适用于字体颜色

extension UILabel {
    func highlight(text: String?, font: UIFont? = nil, color: UIColor? = nil) {
        guard let fullText = self.text, let target = text else {
            return
        }

        let attribText = NSMutableAttributedString(string: fullText)
        let range: NSRange = attribText.mutableString.range(of: target, options: .caseInsensitive)
        
        var attributes: [NSAttributedString.Key: Any] = [:]
        if let font = font {
            attributes[.font] = font
        }
        if let color = color {
            attributes[.foregroundColor] = color
        }
        attribText.addAttributes(attributes, range: range)
        self.attributedText = attribText
    }
}

答案 3 :(得分:0)

更新@ matt的好答案,Swift 4:

 let s = "Eat @my shorts" as NSString
 let att = NSMutableAttributedString(string: s as String)
 let r = s.range(of: "@\\w.*?\\b", options: .regularExpression, range: NSMakeRange(0,s.length))
    if r.length > 0 { att.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: r)
        }