EDIT: I’m thinking I should use a UILabel instead of a UITextView as I don’t want the highlight to use the system wide blue with ‘copy/select all/define’ popover.
I’m trying to build a custom text view, and I’m looking for some help with the right approach. I’m an iOS n00b so mainly looking for ideas about how best to approach the problem.
I want to build a custom text view, with the following behaviour:
- View starts off small. If it receives a tap it grows (animated) to the size of the parent view as a modal window (top left picture, then top right picture)
- In this large state, if an individual word is tapped, the word highlights in some fashion, and a delegate method is called passing a string containing the that was tapped (bottom left picture)

(source: telliott.net)
I suspect the complexity is going to be in the identifying the word that was clicked, so let’s start there. I can think of a couple of ways of trying this:
- Subclass UILabel. Add a touch gesture recognizer. When a touch is identified, get the (x.y) coordinates of the touch point somehow, look at the string the view is showing and figure out which word must have been pressed based on position. I can see this getting very complicated pretty quickly with word wrapping however. I’m not sure how to get the (x,y) coordinates of the touch (although I assume that’s pretty simple), or how to get device dependant text widths for each character etc. I’d rather not go down this route unless someone can convince me it’s not going to be horrible!
- Subclass UIView, and fake the sentence by adding a UILabel for each word. Measure the width of each UILabel and lay them out myself. This seems like a more sensible approach, although I worry that laying out the text in this way is also going to be harder than I think by the time I start trying.
Is there a more sensible way? Can you retrieve a word that was touched in a UILabel some other way?
If I go for option 2, then I think the animation from small -> large text might be complicated, as the words will wrap in interesting ways. So I was thinking of having another subview – this time a UITextView – to hold the sentence in the small state. Animate this to large, then hide it, and at the same time reveal my UIView with one-view-per-word.
Any thoughts appreciated. Thanks in advance 🙂
Update
This got a lot easier for iOS 7, thanks to the addition of NSLayoutManager in CoreText. If you’re dealing with a UITextView you can access the layout manager as a property of the view. In my case I wanted to stick with a UILabel, so you have to create a layout manager with the same size, i.e:
Now you just need to find the index of the character that was clicked, which is simple!
Which makes it trivial to find the word itself:
Original Answer, which works for iOS < 7
Thanks to @JP Hribovsek for some tips getting this working, I managed to solve this well enough for my purposes. It feels a little hacky, and likely wouldn’t work too well for large bodies of text, but for paragraphs at a time (which is what I need) it’s fine.
I created a simple UILabel subclass that allows me to set the inset value:
Then I created a UIView subclass that contained my custom label, and on tap constructed the size of the text for each word in the label, until the size exceeded that of the tap location – this is the word that was tapped. It’s not prefect, but works well enough for now.
I then used a simple NSAttributedString to highlight the text: