Alex Rivera | Logout

UITextView (editing) - detecting that next line event occured

Asked 2011-02-02T11:16:36.487
9

I have a UITextView which is editable. Now I have a requirement which requires me to find (as and when I keep typing) as to when the next line has begun (maybe due to me hitting the return key, or an auto wordwrap linebreak). Is there any notification which can be obtained to figure out when the next line has started while typing?

I tried searching for solutions to find out cursor positions within a textview but using selectedRange and the location properties to find it out does not help me. There is no correlation at all between the location value and a new line. The location value just keeps increasing on typing. Any ideas?

Thanks!

Edit
Report

2 Answers

11

The following delegate is called whenever a new text is entered in textView.

Set the delegate for UITextView, then code as follows

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    if ( [text isEqualToString:@"\n"] ) {
        //Do whatever you want
    }
    return YES;
}
answered 2011-02-02T12:16:56.933
0
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text
{

    if ([text isEqualToString:@"\n"]) {
        textView.text=[NSString stringWithFormat:@"%@\n",textView.text];
        // Return FALSE so that the final '\n' character doesn't get added
        return NO;
    }
    // For any other character return TRUE so that the text gets added to the view
    return YES;
}
answered 2013-03-02T05:06:24.700

Your Answer