This is how I would do it:
- Get the
UITextPosition of the last character.
- Call
caretRectForPosition on your UITextView.
- Create a
CGRect variable and initially store CGRectZero in it.
- In your
textViewDidChange: method, call caretRectForPosition: by passing the UITextPosition.
- Compare it with the current value stored in the
CGRect variable. If the new y-origin of the caretRect is greater than the last one, it means a new line has been reached.
Sample code:
CGRect previousRect = CGRectZero;
- (void)textViewDidChange:(UITextView *)textView{
UITextPosition* pos = yourTextView.endOfDocument;//explore others like beginningOfDocument if you want to customize the behaviour
CGRect currentRect = [yourTextView caretRectForPosition:pos];
if (currentRect.origin.y > previousRect.origin.y){
//new line reached, write your code
}
previousRect = currentRect;
}
Also, you should read the documentation for UITextInput protocol reference here. It is magical, I'm telling you.
Let me know if you have any other issues with this.