How to change color of a word in UITextView?

2019-06-02 21:20发布

问题:

Is it possible to change UITextView`s color in particular range or particular word? If a word appear more than once in a view then change the color of that word.

回答1:

You'll need to use NSAttributedString. I recommend checking out AliSoftware's OHAttributedLabel which extends UILabel and adds support for setting text via NSAttributedString among other helpers. I'm using it in a current project and it's pretty fantastic.



回答2:

I solve the problem. I created subclass of UILable and UITextView used core text (CATextLayer) as a layer on label/textview and NSAttributedString will store all the information about the font and color