问题
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