How to check for return key tap in text view?

How would I check for a return key tap on the keyboard when a text view is first responder? So far I think I have to check the text parameter in the shouldChangeTextIn callback method of UITextViewDelegate. How would I check whether text is a return?

Accepted Reply

A UITextView has a delegate. The delegate has the method textViewDidChange: Look at the last character and determine if it is "\n"


Use this type of code:

-(void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length>1)
         if([[textView.text substringFromIndex:textView.text.length-1] isEqualToString:@"\n"]){
             //  the textView text ended with a return character
         }
}

Replies

hi,


you might want to look back on "Understanding UITextView Input." there's discussion there about the shouldChangeTextIn method and, what i think, is the suggestion to avoid asking for checking specifically about a return key tap.


hope that helps,

DMG

Did you try to set "Auto enable return key" ON in IB ?


That's in the Text Input Traits section of Attributes Inspector.

Yes. I am not sure what that does. I can't find documentation on it.

I found the documentation. That's not what I need.

A UITextView has a delegate. The delegate has the method textViewDidChange: Look at the last character and determine if it is "\n"


Use this type of code:

-(void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length>1)
         if([[textView.text substringFromIndex:textView.text.length-1] isEqualToString:@"\n"]){
             //  the textView text ended with a return character
         }
}

Use the UITextFieldDelegate and implement textFieldShouldReturn. It is called when the user presses the return key on the keyboard.