ios - UITextField 中的 UITextPosition

标签 ios uitextfield uitextrange uitextposition

有什么方法可以让我通过文本字段的 UITextRange 对象获得 UITextField 的当前插入符号位置? UITextField 返回的 UITextRange 是否有任何用处? UITextPosition 的公共(public)接口(interface)没有任何可见成员。

最佳答案

我昨晚遇到了同样的问题。事实证明,您必须在 UITextField 上使用 offsetFromPosition 来获取所选范围“开始”的相对位置才能计算出位置。

例如

// Get the selected text range
UITextRange *selectedRange = [self selectedTextRange];

//Calculate the existing position, relative to the beginning of the field
int pos = [self offsetFromPosition:self.beginningOfDocument 
                        toPosition:selectedRange.start];

我最终使用了 endOfDocument,因为在更改文本字段后更容易恢复用户的位置。我在这里写了一篇博客:

http://neofight.wordpress.com/2012/04/01/finding-the-cursor-position-in-a-uitextfield/

关于ios - UITextField 中的 UITextPosition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9766077/

相关文章:

ios - Swift - App store reject - 部分应用转发器以重新抽象 thunk helper

ios - 运行葫芦测试时如何模拟后端交互

ios - 将输入到 UITextField 中的内容转换为计数器编号,例如 00 :01

ios - 将 selectedTextRange UITextRange 转换为 NSRange

objective-c - Objective C 如何获取 UITextPosition 的数值

c# - Xamarin iOS UIButton 如何以编程方式单击按钮?

ios - 使用 UIImageRenderingModeAlwaysTemplate 从界面生成器中为 UIButton 的图像着色

iphone-sdk : Adding a textfield to UIAlertview does not work in iOS 4?

iphone - 如何根据 UILabel 中的数字显示文本

ios - 为什么 characterRange(at : . zero) == nil?