1

我正在尝试使用其属性(从协议UITextField中获得)来建立实例的文本选择的开始和结束位置。但是,我不知道如何访问组成和属性的对象的属性。selectedTextRangeUITextInputUITextPositionstartendselectedTextRange

Apple 在 UITextPosition 上的文档目前很糟糕,没有提供任何方法或属性,尽管我知道对象中有这样的属性,因为 NSLogging 给出了这个:

<UITextPositionImpl: 0x6aaeb60>
<<WebVisiblePosition: 0x6aa40e0>(offset=5, context=([s|a], [u+0073|u+0061])>

在这个例子中,“偏移”是正确的,上下文显示了选择点两侧的字符(“s”和“a”),但我不知道如何访问这个模糊WebVisiblePosition的类。所以,简而言之,有没有一种方法可以使用UITextPosition来自的对象检索我想要的详细信息UITextField

4

1 回答 1

2

Of course, just after asking my question I found the answer, in this SO question: UITextPosition in UITextField.

It seems that when used as part of UITextField, the UITextPosition objects are not meant to be tinkered with directly, but used to feed other methods. In this case, the method offsetFromPosition:toPosition:, along with the text field property beginningOfDocument, can be used to return an NSInteger of a selection index.

于 2012-04-13T14:42:52.327 回答