Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting a word in a UITextView
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>I want to build a custom text view, with the following behaviour:</p> <ol> <li>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)</li> <li>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)</li> </ol> <p><a href="http://telliott.net/static/NewTextView.png" rel="noreferrer" title="New view">New Text View http://telliott.net/static/NewTextView.png</a></p> <p>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:</p> <ol> <li>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!</li> <li>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.</li> </ol> <p>Is there a more sensible way? Can you retrieve a word that was touched in a UILabel some other way?</p> <p>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.</p> <p>Any thoughts appreciated. Thanks in advance :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload