Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UITextRange to get substring from a UITextField

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.

like image 325
coolio Avatar asked Nov 22 '25 13:11

coolio


1 Answers

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.

like image 125
Jesse Rusak Avatar answered Nov 25 '25 05:11

Jesse Rusak