how can I change style of pre-selected words in my textView?

风流意气都作罢 提交于 2019-12-05 16:07:16

I believe you are using regex to get an array of NSRange. Here, you need a slightly different datastructure like [String : [NSRange]]. Then you can use rangeOfString function to detect the NSRange where the word is located. You can follow the example given below for that:

let wordMatchArray:[String] = ["those", "words", "are", "highlighted"]
let labelText:NSString = NSString(string: "those words, those ldsnvldnvsdnds, are, highlighted,words are highlighted")
let textLength:Int = labelText.length

var dictionaryForEachWord:[String : [NSRange]] = [:]

for eachWord:String in wordMatchArray {

   var prevRange:NSRange = NSMakeRange(0, 0)
   var rangeArray:[NSRange] = []

   while ((prevRange.location + prevRange.length) < textLength) {

      let start:Int = (prevRange.location + prevRange.length)
      let rangeEach:NSRange = labelText.range(of: eachWord, options: NSString.CompareOptions.literal, range: NSMakeRange(start, textLength-start))
      if rangeEach.length == 0 {
         break
      }
      rangeArray.append(rangeEach)
      prevRange = rangeEach
   }

   dictionaryForEachWord[eachWord] = rangeArray
}

Now that you have an array of NSRange i.e, [NSRange] for each word stored in a dictionary, you can highlight each word accordingly in your UITextView.

Feel free to comment if you have any doubts regarding the implementation :)

For this new requirement you don't need a regex, you can just iterate over your array of words and use rangeOfString to find out if that string exists and set the attributes for the located range.

To match the original functionality, after you find a matching range you need to search again, starting from the end of that range, to see if there is another match later in your source text.

The proposed solutions so far suggest that you go through each word and then search them in the text view. This works, but you are traversing the text way too many times.

What I would suggest is to enumerate all the words in the text and see if they match any of the words to highlight:

class ViewController: UIViewController {

    @IBOutlet var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegate = self
        highlight()
    }

    func highlight() {
        guard let attributedText = textView.attributedText else {
            return
        }
        let wordsToHighlight = ["those", "words", "are", "highlighted"]
        let text = NSMutableAttributedString(attributedString: attributedText)
        let textRange = NSRange(location: 0, length: text.length)
        text.removeAttribute(NSForegroundColorAttributeName, range: textRange)

        (text.string as NSString).enumerateSubstrings(in: textRange, options: [.byWords]) { [weak textView] (word, range, _, _) in
            guard let word = word else { return }
            if wordsToHighlight.contains(word) {
                textView?.textStorage.setAttributes([NSForegroundColorAttributeName: UIColor.red], range: range)
            } else {
                textView?.textStorage.removeAttribute(NSForegroundColorAttributeName, range: range)
            }
        }
        textView.typingAttributes.removeValue(forKey: NSForegroundColorAttributeName)
    }
}

extension ViewController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        highlight()
    }
}

This should be fine for small texts. For long texts, going through everything on each change can really hurt performance. In that case, I'd recommend using a custom NSTextStorage subclass. There you would have better control over what range of text have changed and apply the highlight only to that section.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!