Harikrishnan
Harikrishnan

Reputation: 8065

Get exact cursor position from UITextView

I have a UITextView in which I need to insert the value at certain positions. For the purpose I need to get the cursor position. After a bit of browsing 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 output like the one below:

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

From the above set, all I need to get is just the value 4. I couldn't see any method that can give me the value 4(which is the exact cursor position that I need). Is there any way that I can get the value?

Upvotes: 4

Views: 1973

Answers (1)

Leo Chapiro
Leo Chapiro

Reputation: 13984

Try this:

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

Upvotes: 5

Related Questions