Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Done key in iPhone app that closes keyboard (with UITextView)

I'm working on an iPhone app and in the iPhone app the user is typing in an UITextView. I was wondering how I could add the done key on the keyboard instead of having the normal return key and when you click on that key the keyboard closes.

Thanks!

like image 845
Tapy Avatar asked Jan 15 '11 20:01

Tapy


1 Answers

There is no -textViewShouldReturn: method in the UITextViewDelegate protocol. If you want the return (done) key to dismiss the keyboard, it's probably best to use a UITextField instead, UITextView is intended for editing multiple lines of text (so you need a way to enter a linebreak).

If you really want to dismiss the keyboard when hitting the return key in a UITextView, you could probably do something like this:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  if ([text isEqual:@"\n"]) {
    [textView resignFirstResponder];
    return NO;
  }
  return YES;
}
like image 192
omz Avatar answered Sep 19 '22 13:09

omz