How to dismiss keyboard for UITextView with return key?
Figured I would post the snippet right here instead:
Make sure you declare support for the UITextViewDelegate
protocol.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES;}
Swift 4.0 update:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if text == "\n" { textView.resignFirstResponder() return false } return true}
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.
There might be other ways but I am not aware of any.
I know this has been answered already but I don't really like using the string literal for the newline so here is what I did.
- (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if( [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound ) { return YES; } [txtView resignFirstResponder]; return NO;}
Swift 4.0 update:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {if (text as NSString).rangeOfCharacter(from: CharacterSet.newlines).location == NSNotFound { return true}txtView.resignFirstResponder()return false}