Get exact cursor position from UITextView

I have a UITextView in which I need to insert a value at specific positions. To do this, I need to get the cursor position. After a little look, I came up with the following property to show the cursor position:

NSLog(@"%@", textView.selectedTextRange.start); 

I used NSLog to find out in what format I get the output of the above expression. I got the result as below:

 <UITextPosition: 0x7472ed0, 4, {"6554"}, {"333"}> 

From the set above, all I need to get is just the value 4 . I have not seen any method that can give me a value of 4 (this is the exact cursor position that I need). Is there any way to get the value?

+4
source share
1 answer

Try the following:

 // Pposition relative to the beginning of the field int pos = [self offsetFromPosition:self.beginningOfDocument toPosition:textView.selectedTextRange.start]; 
+5
source

Source: https://habr.com/ru/post/1500273/


All Articles