So I am using selectedTextRange to get a cursor position from my UITextField. I would like to obtain a substring from the beginning of my TextField text up to the cursor position. How would I go about doing this? I am able to create the UITextRange I want but I don’t know how to use this to get the actual substring since substringWithRange seems to only work with NSRange. Any guidance would be greatly appreciated.
Share
You need to use the methods from the
UITextInputprotocol, which is implemented by all text views, to do this. With a text range, you can simply calltextInRange:to get the text.To get the text from the beginning of the field up to the selected position, you’ll need to make a text range first with
textRangeFromPosition:toPosition:, passingbeginningOfDocumentfor the first argument, and thestartof your text range for the second argument.