iOS基础控件1111iOS开发

UITextField、UITextView代理方法罗列

2016-12-21  本文已影响2144人  liailing

有时候代理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; //允许继续输入
}
上一篇下一篇

猜你喜欢

热点阅读