有時候代理UITextFieldDelegate、UITextViewDelegate給出的方法無法滿足我們的需求耀里,所以出現(xiàn)了一些通知:
UITextFieldTextDidBeginEditingNotification; //開始輸入
UITextFieldTextDidEndEditingNotification; //停止輸入
UITextFieldTextDidChangeNotification; //輸入的內(nèi)容改變
UITextViewTextDidBeginEditingNotification;
UITextViewTextDidChangeNotification;
UITextViewTextDidEndEditingNotification;
UITextFieldDelegate:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; //return NO鍵盤不彈出
- (void)textFieldDidBeginEditing:(UITextField *)textField; //成為第一響應(yīng)者
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; //return YES 鍵盤關(guān)閉
- (void)textFieldDidEndEditing:(UITextField *)textField; //取消第一響應(yīng)者
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); //給出結(jié)束編輯的原因( UITextFieldDidEndEditingReasonCommitted,
UITextFieldDidEndEditingReasonCancelled)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; //return NO文本框內(nèi)不可以再改變文本內(nèi)容(一般用于限制輸入)
- (BOOL)textFieldShouldClear:(UITextField *)textField; //return YES 清除文本(點擊清除按鈕時響應(yīng))
- (BOOL)textFieldShouldReturn:(UITextField *)textField; //return YES 點擊鍵盤return鍵時的響應(yīng)
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改變內(nèi)容后(一般用于計算文本的高度)
- (void)textViewDidChangeSelection:(UITextView *)textView; //第一次啟動TextView編輯或者手動調(diào)整光標位置時觸發(fā),在這里可以區(qū)別是哪個TextView啟動了鍵盤.它的優(yōu)先級高于 textViewDidBeginEditing方法 也高于 UIKeyboardWillShowNotification 通知 一般用于選中該UITextView中某些文本時響應(yīng)
(修改光標位置 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); //指定范圍的內(nèi)容與 URL 將要相互作用時激發(fā)該方法——該方法隨著 IOS10被使用;
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction NS_AVAILABLE_IOS(10_0); //指定范圍的內(nèi)容與文本附件將要相互作用時,自動激發(fā)該方法——該方法隨著 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已存在的內(nèi)容拾氓, text即將輸入的內(nèi)容
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]; //取消第一響應(yīng)者,收回鍵盤
return NO; //點擊鍵盤return鍵后不允許再輸入
}
return YES; //允許繼續(xù)輸入
}