iOS UITextView dismiss keyboard
使用者在 UITextView 輸入完畢後,無法將鍵盤關閉,
以下方法,是透過 keyboard 的 return key 去關閉鍵盤
Quick Start
繼承UITextViewDelegate
1
| @interface TestViewController () <UITextViewDelegate>
|
設定UITextView delegate
1 2 3 4 5
| - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.txTextView.delegate = self; }
|
實作 UITextView shouldChangeTextInRange
1 2 3 4 5 6 7 8
| - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; }
|