UITextView textViewShouldEndEditing

   

最近做到UITextView, 在取消键盘事件上我以为和UITextField差不多,于是我这样写:

1  UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 40, 280, 240)];
2  [textView setBackgroundColor:[UIColor greenColor]];
3  [textView setFont:[UIFont fontWithName:@"MyriadPro-Regular" size:13]];
4  [textView setTextColor:[UIColor blackColor]];
5  [textView setText:@"Your Message...."];
6  [textView setBackgroundColor:[UIColor clearColor]];
7  [textView setDelegate:self];
8  [textView setReturnKeyType:UIReturnKeyDone];

 

    我以为,当按下键盘上的“完成”按钮后,将被调用

1 - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
2  NSLog(@"called");
3  [textView resignFirstResponder];
4  return YES;
5 }

 

然而我忘记了textView有换行的事件,即

   点击Return,它只是在textview里换行。所以,如果你想捕捉到换行\n时取消响应resignFirstResponder,可以这样

1 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
2  if ( [text isEqualToString:@"\n"] ) {
3   [textView resignFirstResponder];
4  }
5  return YES;
6 }


posted on 2015-07-16 13:30  MichaelMao  阅读(803)  评论(0编辑  收藏  举报