(iPhone)selectedRange为不可编辑的UITextView(或其他点击处理方法?) [英] (iPhone) selectedRange for a non-editable UITextView (or other methods of click handling?)

查看:600
本文介绍了(iPhone)selectedRange为不可编辑的UITextView(或其他点击处理方法?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 UITextView 保存静态内容(中文,这意味着字符都是固定宽度)。我想让用户点击文本中的一个字符,并为该字符提供字典信息。我知道所有的问题,围绕缺乏复制和粘贴和所有这一切,但我希望有一个方法可以做到这一点,而无需等待iPhone 3.0固件。

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.

首先,我考虑使用 UITextView s selectedIndex 属性。不幸的是,当 UITextView 不可编辑时, UITextView 中的以下代码总是返回整个文本块:

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);

我认为这是有意义的,如同一个不可编辑的 UITextView 没有办法选择一个插入点,但它不帮助我任何。 :)

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?

推荐答案

在请求 selectedRange 之前调用以下代码:

Call the following before asking for the selectedRange:

[[textView webView] updateSelectionWithPoint:point];

注意: -webView -updateSelectionWithPoint:是私有API。你也可以通过切换可编辑,成为第一响应者和发送假触摸事件来执行等效行为,但这将是更多的工作

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

这篇关于(iPhone)selectedRange为不可编辑的UITextView(或其他点击处理方法?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆