UITextView textviewshouldenditing never called

I have a UITextView setting as follows:

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

I expect that when the user clicks the Finish button on the keyboard, this method will be called (which I implemented):

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

But this method is never called. What am I doing wrong? Thanks.

0
source share
1 answer

As long as you set the type of return key, it does not change the behavior of the text view. In Return it will add a new line to the text view. Therefore, if you do not want your textual representation to be multi-line, you can capture \n and resignFirstResponder .

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

On the side of the note, textViewShouldEndEditing: is called after you have canceled your status as the first responder.

If you want to keep newline characters in a text view, you should consider using the inputAccessoryView text view. An example of this is here .

+4
source

All Articles