views:

1226

answers:

1

Hello all,

I'm using an UITextView to hold static content (in Chinese, which means that the characters are all fixed width). I want to let users click on a character in the text and bring up dictionary information for that character. I know all of the issues surrounding the lack of copy and paste and all that, but I'm hoping that there will be a way to do this without waiting for the iPhone 3.0 firmware.

At first, I thought about using UITextViews selectedIndex property. Unfortunately, when the UITextView is not editable, the following code in the UITextView's always returns the length of the entire text block:

NSRange touchPoint = self.selectedRange;
NSLog(@"Selection: %d", touchPoint.location);

This makes sense, I suppose, as with a non-editable UITextView there's no way to select an insertion point, but it's doesn't help me any. :)

Another approach would be to calculate, using the fact that the Chinese text is fixed width, where the click landed and what text should be under that location at the time, but this is complicated by punctuation which can cause a line to wrap early, bringing preceding characters down a line.

Is there another way of know what text is under a touch event that I'm missing?

+1  A: 

Call the following before asking for the selectedRange:

[[textView webView] updateSelectionWithPoint:point];

Note: Both -webView and -updateSelectionWithPoint: are private APIs. You can alsoperform the equivalent behavior by toggling editable, becoming first responder and sending fake touch events, but that would be much more work

rpetrich
Just tried it. Works wonderfully if the UITextView is editable (don't have to hold the touch until the magnifier pops up, etc.), but still returns the length of the text when the UITextView is made non-editable. Will have to try the longer fake event version later.
John Biesnecker
Perhaps setting editable before and after will work. Of course if you are just showing plaintext, a better way would be to simply use a custom view that draws text manually and you will have full control
rpetrich