【问题标题】:Applying Text Attributes to ALL Occurrence of certain string将文本属性应用于特定字符串的所有出现
【发布时间】:2015-07-28 16:57:23
【问题描述】:

我正在应用string“更新-”的文本属性。

var attributedText: NSMutableAttributedString = NSMutableAttributedString(string: textView.text)
             let rangeOfString = (textView.text as NSString).rangeOfString("Updated - ")
             attributedText.addAttributes([NSFontAttributeName: UIFont.italicSystemFontOfSize(8.0)],range: rangeOfString)
             textView.attributedText = attributedText

我希望这适用于string所有次出现,而不仅仅是一个

试过了

var attributedText: NSMutableAttributedString = NSMutableAttributedString(string: textView.text)
             let stringRange = (textView.text as NSString).rangeOfString("Updated - ")


             if (stringRange.location != NSNotFound) {

                    attributedText.addAttributes([NSFontAttributeName: UIFont.italicSystemFontOfSize(8.0)],range: stringRange)
                    textView.attributedText = attributedText

            }

这仍然只将属性文本应用于string 的一次出现

类似问题Color all occurrences of string in swift

更新

var attributedText: NSMutableAttributedString = NSMutableAttributedString(string: textView.text)
             var inputLength = count(attributedText.string)
             let stringRange = "Updated - "

             var searchLength = count(stringRange)
             var range = NSRange(location: 0, length: attributedText.length)

             while (range.location != NSNotFound) {

                range = (attributedText.string as NSString).rangeOfString("Updated - ", options: nil, range: range)

                if (range.location != NSNotFound) {
                     attributedText.addAttribute(NSForegroundColorAttributeName, value: UIFont.italicSystemFontOfSize(8.0), range: NSRange(location: range.location, length: searchLength))
                    range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
                }
             }

 textView.attributedText = attributedText

【问题讨论】:

    标签: ios string swift


    【解决方案1】:

    您必须遍历文本中“已更新 -”的所有实例并获取它们的范围。一种方法是这样(根据this answer 中的问题量身定制):

    var attributedText: NSMutableAttributedString = NSMutableAttributedString(string: textView.text)
    
    while (range.location != NSNotFound) {
        range = (attributedText.string as NSString).rangeOfString("Updated - ", options: nil, range: range)
        if (range.location != NSNotFound) {
             attributedText.addAttributes([NSFontAttributeName: UIFont.italicSystemFontOfSize(8.0)],range: range)
            range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
        }
    }
    
    textView.attributedText = attributedText
    

    【讨论】:

    • 已更新,仍无法按预期工作,现在没有归属
    • @JSA986 你必须在 textView 上设置属性属性。我还更新了 .addAttribute 以匹配您想要的效果。现在应该可以了。
    • 还是一样,文字完全没有属性-[UICTFont set]: unrecognized selector sent to instance
    • @JSA986 我认为 .addAttributes 中有错误。我纠正了我相信。试一试。
    • 太棒了,感谢您的帮助,加 1!
    【解决方案2】:

    这里的代码返回字符串出现范围的数组。我使用了 JustAnotherCoder 的代码。然后你可以遍历范围。我只是将代码概括为其他用途。

    斯威夫特 3

    func rangesOf(subString: String) -> [NSRange] {
        var ranges = [NSRange]()
    
        var range: NSRange = NSMakeRange(0, self.characters.count)
        while (range.location != NSNotFound) {
            range = (self as NSString).range(of: subString, options: .caseInsensitive, range: range)
            if (range.location != NSNotFound) {
                ranges.append(range)
                range = NSRange(location: range.location + range.length, length: self.characters.count - (range.location + range.length))
            }
        }
        return ranges
    }
    

    【讨论】:

      【解决方案3】:

      使用字符串扩展更新了@Khaled Annajar 对 Swift 4 的回答:

      extension String {
      
          func ranges(subString: String) -> [NSRange] {
              var ranges = [NSRange]()
              var range = NSRange(location: 0, length: count)
              while range.location != NSNotFound {
                  range = (self as NSString).range(of: subString, options: .caseInsensitive, range: range)
                  if range.location != NSNotFound {
                      ranges.append(range)
                      range = NSRange(location: range.location + range.length,
                                  length: count - (range.location + range.length))
                  }
              }
              return ranges
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-10
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多