有什么方法可以让我通过文本字段的 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/