【问题标题】:Highlighting a substring in a UILabel突出显示 UILabel 中的子字符串
【发布时间】: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


【解决方案1】:

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

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

希望能提供线索...

【讨论】:

  • 不使用@我想让整个字符串变成红色?
【解决方案2】:

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

}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      Swift5 版本适用于 FontColor

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-01
        • 2013-10-21
        • 1970-01-01
        • 2015-01-30
        • 1970-01-01
        • 2021-09-12
        • 1970-01-01
        • 2020-10-18
        相关资源
        最近更新 更多