27 Answers
27

UITextView does not have any methods which will be called when the user hits the return key. If you want the user to be able to add only one line of text, use a UITextField. Hitting the return and hiding the keyboard for a UITextView does not follow the interface guidelines.

Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.

Thanks and the method works well. The reason to use UITextView is that it can hold text in multiple lines. And now I'm using it as a message box.
–
Chilly ZhongApr 1 '09 at 6:12

20

It is easy enough to change the return key to "done" using either [textField setReturnKeyType: UIReturnKeyDone]; or using interface builder
–
CasebashSep 28 '10 at 2:58

7

Okay, I now understand that the Apple way of finishing with a multiline text field is to add done to the menu bar
–
CasebashSep 28 '10 at 3:09

4

This is not a good way to resolve this because you're restricting user to use enter to go out from keyboard. Probably the best way is adding a button that execute resignFirstResponder method.
–
Eleazar EnriqueNov 7 '13 at 21:56

1

Try using the UIKeyboard accessory view and adding a button that resigns the keyboard.
–
Braydon BatungbacalNov 9 '13 at 14:18

The trouble is that this is not really a way of detecting that the user has tapped the return key. The user might paste a return character into the text field and you'd get the same delegate message. Basically you're misusing the text view. The way to dismiss the keyboard is (1) not to do so at all (as when composing a message in the Mail app) or (2) to have a Done button elsewhere in the interface (as in the Notes app). You could attach such a button as an accessory view to the keyboard, for example.
–
mattDec 23 '10 at 18:18

I might modify it like this: NSRange resultRange = [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet] options:NSBackwardsSearch]; Because this is a hack anyway, it seems checking the end of the string for a return might be a safer route.
–
maxpowerMay 7 '13 at 14:50

A more elegant way is to dismiss the keyboard when the user taps somewhere outside of the keyboard's frame.

First, set your ViewController's view to the class "UIControl" in the identity inspector in UIBuilder. Control-drag the view into the ViewController's header file and link it as an action with the event as Touch Up Inside, such as:

You can require a double tap or long touch using similar techniques. You may need to set your ViewController to be a UITextViewDelegate and connect the TextView to the ViewController. This method works for both UITextView and UITextField.

Source: Big Nerd Ranch

EDIT: I'd also like to add that if you are using a UIScrollView, the above technique may not work as easily through the Interface Builder. In that case, you could use a UIGestureRecognizer and call the [[self view] endEditing:YES] method within it instead. An example would be:

I know this has been answered a lot of times, but here are my two cents to the issue.

I found the answers by samvermette and ribeto really useful, and also the comment by maxpower in the ribeto's answer. But there is a problem with those approaches. The problem that matt mentions in the samvermette's answer and it's that if the user wants to paste something with a line break inside it, the keyboard would hide without pasting anything.

So my approach is a mixture of the three above mentioned solutions and only checking if the string entered is a new line when the length of the string is 1 so we make sure the user is typing instead of pasting.

Off topic. While your solution is elegant, it does not respond to the original question: "How to dismiss keyboard for UITextView with return key?". There are situations when UITextView is employed to simulate a word wrapping UITextField, not to enter multiple lines.
–
Lancelot de la MareJul 16 '12 at 10:51

2

Although it is off topic, it is very useful. I also want a UITextView to be entered with multiple line and dismiss keyboard when I want to.
–
YeungOct 4 '12 at 4:37

Err it does use \n and return key is detected based on \n so it does check for return key. The only difference is that you are using notifications rather than using the textViewDelegates.
–
GoodSp33dJan 26 at 14:57

Now I think checking for [NSCharacterSet newlineCharacterSet] rather than \n might be a better way to go.
–
ToddBJan 26 at 18:34

There is another solution while using with uitextview,
You can add toolbar as InputAccessoryView in "textViewShouldBeginEditing", and from this toolbar's done button you can dismiss keyboard, the code for this is following:

Step 2. Next you will need to register the controller as the UITextView’s delegate. Continuing from the example above, here is how I have initialize the UITextView with EditorController as the delegate …

Step 3. And now the final piece of the puzzle is to take action in response to the shouldCahngeTextInRange message as follows:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}