Feb 19

In a previous post I presented a framework for creating a data entry screen. That code focused mainly on UITextFields and allowed the user to move to the next field by tapping the Return key on the keyboard.

What if you have one or more UITextViews on your data entry screen because you want the user to be able to enter multiple lines of text? Well the UITextView handles the Return key internally and adds a new line to the text being entered, just like you would expect when you’re writing text in a text editor. And there is no equivalent of textFieldShouldReturn for UITextView.

If you don’t care about newlines in the entered text and you just want to exit the field when the Return key is tapped, then add the following method to the UITextViewDelegate:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  BOOL shouldChangeText = YES;
	
  if ([text isEqualToString:@"\n"]) {
    // Find the next entry field  
    BOOL isLastField = YES;
    for (UIView *view in [self entryFields]) {  
      if (view.tag == (textView.tag + 1)) {  
        [view becomeFirstResponder];  
        isLastField = NO;
        break;  
      }  
    }  
    if (isLastField) {
      [textView resignFirstResponder];
    }
	
    shouldChangeText = NO;
  }
	
  return shouldChangeText;
} 

The shouldChangeTextInRange method is called after each keystroke is received by the UITextView, but before it’s shown on screen. If the text is a “\n” character (the Return key) then we either set first responder to the next field, or hide the keyboard in the case that this is the last field. (The entryFields method is explained in the previous post.) Return NO if a return character was detected since we don’t want the new line to show up in the UITextView, otherwise return YES and the text will be processed and shown in the UITextView as normal.

written by Nick \\ tags: ,

6 Responses to “Handling the Return Key in a UITextView”

  1. Frank Maroney Says:

    Thank you, very helpful!

  2. Larry Says:

    Thank you, that’s help me a lot !

  3. nick Says:

    Hi, great little piece, really helpful; though I think this would be a bit more portable as it covers all newline variants. You can easily ass the next responder code in to the else.

    -(BOOL) textView:(TableTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
    if( [text isEqualToString:[text stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]] ) {
    return YES;
    } else {
    [textView resignFirstResponder];
    }
    }

  4. Thompson Says:

    I like this method and use it extensively, but how can a user exit the text field w/out entering data? The return key on the default keyboard is default disabled if there is no text…

  5. Thompson Says:

    Ha ha, never mind! After a few days of struggle the answer came to me minutes later. UITextView responds to enablesReturnKeyAutomatically BUT (a) the default value |NO| seems to be ignored in some cases AND (b) setting to |NO| enables the return key, while setting to |YES| disables it… very counter-intuitive!

  6. Amr Elghadban Says:

    for swift version i may introduce the following

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text == text.trimmingCharacters(in: .newlines) {
    return true;
    }else{
    textView.resignFirstResponder()
    return false
    }
    }

Leave a Reply