iProgrammer Tip #5: Dismiss keyboard while entering text in UITextView..

Jun 16 2009 Published by Prakash under iPhone Development Tips

We have managed to Dismiss keyboard from UITextField in response to ‘return’ in keyboard .

But unfortunately, the same trick wont be useful when working with UITextView.

UITextViewDelegate does not have such a method to be delegated. Below is the list of optional methods for UITextViewDelegate

@protocol UITextViewDelegate <NSObject>

@optional

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;

- (BOOL)textViewShouldEndEditing:(UITextView *)textView;

- (void)textViewDidBeginEditing:(UITextView *)textView;

- (void)textViewDidEndEditing:(UITextView *)textView;

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

- (void)textViewDidChange:(UITextView *)textView;

- (void)textViewDidChangeSelection:(UITextView *)textView;

@end

After staring at them for a while, it dawned that shouldChangeTextInRange could be our rescue.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

if([text isEqualToString:@"\n"])

{

[textView resignFirstResponder];

return FALSE;

}

return TRUE;

}

No responses yet