gpt4 book ai didi

ios - UITextField 中的 UITextPosition

转载 作者:IT王子 更新时间:2023-10-29 08:18:19 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com