My UITextView delegate is logging the caret position using the following:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
NSLog(@"cursor: %@", NSStringFromCGPoint(cursorPosition));
}
But the reported position is always inaccurate. Specifically, it reports the previous cursor position - for example, if I click once inside the text view at position (x,y) then outside, then back inside at (x2,y2), on the second click the (x,y) coordinates are logged.
In fact, the selectedTextRange is the issue - the previous range is reported.
What am I missing? I don't see another delegate method I can use instead.
Value of selectedTextRange will be older only as the editing has just began, it has not changed yet. As you are calculating the cursorPostion with selected text, old values will lead to old position. So we need to calculate the new position inside a different delegate which reports after the selection changes. You will get correct readings with below mentioned code. Hope it helps
-(void)textViewDidChangeSelection:(UITextView *)textView
{
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
NSLog(@"cursor start: %@", NSStringFromCGPoint(cursorPosition));
}
It's pretty gross but one solution is to introduce a short delay before reading calling the method:
UITextView *textView = note.object;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
NSLog(@"cursor: %@", NSStringFromCGPoint(cursorPosition));
});
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