iOS - Highlight One Word Or Multiple Words In A UITextView

时光毁灭记忆、已成空白 提交于 2019-12-11 02:42:46

问题


In my project, I'm making a small piece of functionality that allows a user to read a body of text (in a UITextView) faster by having one word being highlighted at a time, or even multiple if a user wants. One by one, each word in the UITextView should be highlighted at the speed the user sets.

Is there any way I could highlight the background of a word in a UITextView? I think it has something to do with attributed strings, but I can't find anything that highlights the background of an NSString.

Any help or advice would be appreciated. Thanks.


回答1:


It does have to do with attributed strings. It has nothing to do with NSString, because an attributed string is not an NSString; it is an NSAttributedString, or its mutable subclass NSMutableAttributedString. What you want to do is set the NSBackgroundColorAttributeName to a background color for the range of the desired word.

I should mention also that you can obtain snazzier effects by intervening in the Text Kit stack that constructs the UITextView's drawing system, as in this image where I'm drawing a rectangle around a word:

However, the background color should be enough to get you started.



来源:https://stackoverflow.com/questions/31441154/ios-highlight-one-word-or-multiple-words-in-a-uitextview

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