有時候代理UITextFieldDelegate、UITextViewDelegate給出的方法無法滿足我們的需求,所以出現了一些通知:
UITextFieldTextDidBeginEditingNotification; //開始輸入
UITextFieldTextDidEndEditingNotification; //停止輸入
UITextFieldTextDidChangeNotification; //輸入的內容改變
UITextViewTextDidBeginEditingNotification;
UITextViewTextDidChangeNotification;
UITextViewTextDidEndEditingNotification;
UITextFieldDelegate:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; //return NO鍵盤不彈出
- (void)textFieldDidBeginEditing:(UITextField *)textField; //成為第一響應者
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; //return YES 鍵盤關閉
- (void)textFieldDidEndEditing:(UITextField *)textField; //取消第一響應者
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); //給出結束編輯的原因( UITextFieldDidEndEditingReasonCommitted,
UITextFieldDidEndEditingReasonCancelled)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; //return NO文本框內不可以再改變文本內容(一般用于限制輸入)
- (BOOL)textFieldShouldClear:(UITextField *)textField; //return YES 清除文本(點擊清除按鈕時響應)
- (BOOL)textFieldShouldReturn:(UITextField *)textField; //return YES 點擊鍵盤return鍵時的響應
UITextViewDelegate:
- (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; //textView改變內容后(一般用于計算文本的高度)
- (void)textViewDidChangeSelection:(UITextView *)textView; //第一次啟動TextView編輯或者手動調整光標位置時觸發,在這里可以區別是哪個TextView啟動了鍵盤.它的優先級高于 textViewDidBeginEditing方法 也高于 UIKeyboardWillShowNotification 通知 一般用于選中該UITextView中某些文本時響應
(修改光標位置 NSRange range; range.location = 0; range.length = 0; textView.selectedRange = range; )
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0); //指定范圍的內容與 URL 將要相互作用時激發該方法——該方法隨著 IOS10被使用;
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0); //指定范圍的內容與文本附件將要相互作用時,自動激發該方法——該方法隨著 IOS10被使用;
//下面這倆我也沒用過,只是列出來
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithTextAttachment:inRange:forInteractionType: instead");
單個的UITextView 使用方法舉例:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if (textView.tag == 100) {
//textView.text已存在的內容, text即將輸入的內容
if (0 < textView.text.length + text.length) {
if (textView.text.length + text.length > 40) {
[MSBase alertMessage:@"只能輸入 40 個字符" cb:nil];
return NO;
}
}
}
if ([text isEqualToString:@"\n"]) { //點擊鍵盤return鍵,相當于textFieldShouldReturn方法
if (lastCellHeigh>58) { //cell高度大于58時刷新Section:0 Row:3 位置的cell
NSIndexPath *indexPathA = [NSIndexPath indexPathForRow:3 inSection:0];
[_mainTableView reloadRowsAtIndexPaths:@[indexPathA] withRowAnimation:UITableViewRowAnimationNone];
}
[textView resignFirstResponder]; //取消第一響應者,收回鍵盤
return NO; //點擊鍵盤return鍵后不允許再輸入
}
return YES; //允許繼續輸入
}