Objective C how to get the numeric value of a UITextPosition Objective C how to get the numeric value of a UITextPosition objective-c objective-c

Objective C how to get the numeric value of a UITextPosition


There is an answer to a similar question here: UITextPosition to NSRange.

Basically you call -offsetFromPosition:toPosition: on the UITextRange's start and end, with an initial position of myTextView.beginningOfDocument.


A UITextView conforms to UITextInput. As part of this protocol it must implement with, and process, its own subclasses of UITextRange and UITextPosition. These are not structs, but objects.

You can get a simple numeric position by using offSetFromPosition:toPosition: and passing in beginningOfDocument to get the start position. All of these methods will be implemented by UITextView. You can also pass in the start and end of the range to find out the length of a selection.

Full protocol reference here.