Ios 高亮显示UILabel中的子字符串

Ios 高亮显示UILabel中的子字符串,ios,uitableview,swift,twitter,Ios,Uitableview,Swift,Twitter,我可以;t突出显示子字符串。以下是我所做的(是一个“重复”问题): 在这里,我需要将其转换为NSRange for user in tweet.userMentions { let userName = user.keyword if let startIndex = tweetTextLabel.text?.rangeOfString("@")?.startIndex { let range = startIndex...userNa

我可以;t突出显示子字符串。以下是我所做的(是一个“重复”问题):

在这里,我需要将其转换为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 我使用的是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)
另一次更新 所以我更新代码的地方仍然没有给子字符串上色
我正在开发一款推特应用程序,该应用程序以颜色标签、URL和用户屏幕名称突出显示

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

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”是红色的


希望这能提供线索…

更新@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)
        }

在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
    }

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

 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
    }

}

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
    }
}

“突出”对你意味着什么?简单地说,你想完成什么?你是在尝试制作一个标签,其中一个单词是红色的吗?是的,我想用不同的颜色给一个标签、用户或url上色。如果不使用@i,我所做的事情不会突出显示整个字符串是红色的吗?