Reputation:
I'm trying to establish the start and end positions of a text selection of a UITextField
instance, using its selectedTextRange
property (as gained from the UITextInput
protocol). However, I have no idea how to access the properties of the UITextPosition
objects that make up the start
and end
properties of selectedTextRange
.
Apple's docs on UITextPosition are woeful at this time, providing no methods or properties, though I know there are such properties in the object, because NSLogging one gives this:
<UITextPositionImpl: 0x6aaeb60>
<<WebVisiblePosition: 0x6aa40e0>(offset=5, context=([s|a], [u+0073|u+0061])>
In this example, the 'offset' is correct, and the context shows the characters either side of the selection point ('s' and 'a'), but I don't know how to access this nebulous WebVisiblePosition
class. So, in short, is there a way of retrieving the details I want using UITextPosition
objects from UITextField
?
Upvotes: 1
Views: 2048
Reputation:
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.
Upvotes: 2