Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pixel-Position of Cursor in UITextView

Is there a way of getting the position (CGPoint) of the cursor (blinking bar) in an UITextView (preferable relative to its content). I don’t mean the location as an NSRange. I need something around:

- (CGPoint)cursorPosition;

It should be a non-private API way.

like image 368
Raphael Schaad Avatar asked Sep 10 '25 16:09

Raphael Schaad


1 Answers

Requires iOS 5

CGPoint cursorPosition = [textview caretRectForPosition:textview.selectedTextRange.start].origin;

Remember to check that selectedTextRange is not nil before calling this method. You should also use selectedTextRange.empty to check that it is the cursor position and not the beginning of a text range. So:

if (textview.selectedTextRange.empty) {
    // get cursor position and do stuff ...
}
like image 59
Craz Avatar answered Sep 12 '25 08:09

Craz