I try to get the UITextView caret position. For this, I use caretRectForPosition
method. It works fine while typing text manually. But if I insert text into the text view, the method returns nonsensical negative coordinate.
Here is subject part of my code:
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// Truncated part of the code: text preparation, objects declaration and so on.
// Past calculated text into the textView
textView.text = newTextViewText;
// Calculate cursor position to avoid its jump to the end of the string. This part works fine.
NSInteger cursorPosition = range.location + allowedText.length;
textView.selectedRange = NSMakeRange(cursorPosition, 0);
// Try to get caret coordinates. It doesn't work properly when text is pasted
cursorCoordinates = [textView caretRectForPosition:textView.selectedTextRange.end].origin;
}
I suppose there is some delay after text insert and the string is been processed when I try to get the cursor coordinates. But I have no Idea where to look for this time gap source. Any Idea?
Update: I found out that this occurs when the inserting text is placed in 2 and more lines. Still don't know how to fix it.