(iPhone) selectedRange for a non-editable UITextView (or other methods of click handling?)

有些话、适合烂在心里 提交于 2019-12-02 07:14:34

问题


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:


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



来源:https://stackoverflow.com/questions/668652/iphone-selectedrange-for-a-non-editable-uitextview-or-other-methods-of-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!