UITextField中的UITextPosition [英] UITextPosition in UITextField

查看:1504
本文介绍了UITextField中的UITextPosition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有没有办法通过文本字段的UITextRange对象获取UITextField当前的插入位置?即使有任何用途,UITextField还是返回了UITextRange吗? UITextPosition的公共接口没有任何可见成员。

Is there any way for me to get at a UITextField's current caret position through the text field's UITextRange object? Is the UITextRange returned by UITextField even of any use? The public interface for UITextPosition does not have any visible members.

推荐答案

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

I was facing the same problem last night. It turns out you have to use offsetFromPosition on the UITextField to get the relative position of the "start" of the selected range to work out the position.

例如

// 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,因为更改文本字段后更容易恢复用户的位置。我在这里写了一篇博客文章:

I ended up using the endOfDocument as it was easier to restore the user's position after changing the text field. I wrote a blog posting on that here:

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

这篇关于UITextField中的UITextPosition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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