突出显示UILabel中的子字符串

Ste*_*rta 3 twitter uitableview ios swift

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

这是我需要将它投射到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
    }
}
Run Code Online (Sandbox Code Playgroud)

我能做什么??也许那里有另一个功能,我快速使用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)
Run Code Online (Sandbox Code Playgroud)

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

mat*_*att 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)
}
Run Code Online (Sandbox Code Playgroud)

这给出了一个属性字符串"Eat @my shorts",其中"@my"这个词是红色的.

在此输入图像描述

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


Mic*_*sen 8

在 swift 4 中寻找这个扩展:

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
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑:扩展改进,他可以接收字符串的参数

 extension UILabel {

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

        let attributeTxt = NSMutableAttributedString(string: txtLabel)

        searchedText.forEach {
            if let searchedText = $0?.lowercased() {
                let range: NSRange = attributeTxt.mutableString.range(of: searchedText, options: .caseInsensitive)

                attributeTxt.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
                attributeTxt.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: self.font.pointSize), range: range)
            }
        }

        self.attributedText = attributeTxt
    }

}
Run Code Online (Sandbox Code Playgroud)