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.
You need to use the methods from the UITextInput protocol, which is implemented by all text views, to do this. With a text range, you can simply call textInRange: 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:, passing beginningOfDocument for the first argument, and the start of your text range for the second argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With