tags:

views:

38

answers:

1

Does anyone know how the built-in Notes app manages to show data detectors and yet still have its UITextView editable?

I've tried defaulting editable to NO (so the data detectors will work) then overriding UITextView's touches methods and setting it to YES, but it ends up scrolling to the bottom and I have to tap again to actually begin editing.

I also tried it with a tap gesture recognizer and the same thing happens. I'm trying to see the touch event in-flight and turn editable to YES so that the UITextView will begin editing at the tap point but no dice - I am not able to get it to pass the touch event through to the UITextView if I mess with its editable property.

A: 

A simple hack would be to put a transparent view over the textview that would intercept touch events. When somebody touches down on the transparency, set the textview to editable and also make it the first responder so that the keyboard shows up.

Then when you resign the first responder status of the keyboard (i.e. user is done editing) set the editable flag back to NO.

[EDIT]

To find the size of a string, so you can figure out where to map a touch/cursor to. Use this:

 CGSize *sizeOfString = [YourString sizeWithFont:[UIFont systemFontOfSize:yourTextViewFontSize]
                               constrainedToSize:CGSizeMake(widthOfYourTextView,
                                                            heightOfYourTextView)
                                   lineBreakMode:UILineBreakModeWordWrap];

[EDIT x2] Have you tried putting two textviews over each other? One being un-editable with data detectors on, the other being editable but with an alpha of 0.0. When text is changed in one, copy the text over to the other.

Andrew
Thanks. I've tried something similar with a UITapGestureRecognizer on the parent view (the UIViewController's view). The problem is that as soon as you set the UITextView's editable property to YES it makes the UITextView the first responder but also moves the insert point to the bottom of the text - I need it to stay where the user tapped.
finsprings
Can't you also programmatically set the selectedRange. i.e. NSMakeRange(indexOfWhereYouWantCursor, 0); You'd have to figure out approximately where they touched as in, how many characters in that was.
Andrew
I tried that too but I wasn't able to figure out how to map a touch location to a selection range.
finsprings
Thanks Andrew. I half-considered the dual text-views before but discounted it. It might work though. I'll give it a go. Thanks for the string size trick too - that looks like it will come in handy.
finsprings