3

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.

Rafi
  • 1,902
  • 3
  • 24
  • 46

1 Answers1

2

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:

enter image description here

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • How will I know the range of each word as I go through the `UITextView`? – Rafi Jul 17 '15 at 22:01
  • That would be a different question. If you have a new question, ask a new question! I believe I've answered the question you did ask — namely, how to "highlight the background". – matt Jul 17 '15 at 22:11
  • True, I'll look into it further. Thanks for the help! :) – Rafi Jul 17 '15 at 22:14
  • @matt could you post the code related to "drawing a rectangle around a word" ! – Saif Sep 01 '16 at 13:36
  • @saif I have posted that code, and I've written a book that explains it. – matt Sep 01 '16 at 15:19