textview constrain count

来源:互联网 发布:大月氏国 知乎 编辑:程序博客网 时间:2024/05/22 15:48
TextView里限制输入字数的方法2011-05-12 10:50 8353人阅读 评论(0) 收藏 举报联想     一开始采用的方法是函数textView:shouldChangeTextInRange:replacementText:来进行判断:[c-sharp] view plaincopy    //键入Done时,插入换行符,然后执行addBookmark      - (BOOL)textView:(UITextView *)textView          shouldChangeTextInRange:(NSRange)range           replacementText:(NSString *)text      {          //判断加上输入的字符,是否超过界限          NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];          if (str.length > BOOKMARK_WORD_LIMIT)          {              textView.text = [textView.text substringToIndex:BOOKMARK_WORD_LIMIT];              return NO;          }          return YES;      }     但在使用中发现该方法在有联想输入的时候,根本无法对联想输入的词进行判断,然后尝试使用textViewDidChange:,验证可行:[cpp] view plaincopy    /*由于联想输入的时候,函数textView:shouldChangeTextInRange:replacementText:无法判断字数,      因此使用textViewDidChange对TextView里面的字数进行判断      */      - (void)textViewDidChange:(UITextView *)textView      {          //该判断用于联想输入          if (textView.text.length > BOOKMARK_WORD_LIMIT)          {              textView.text = [textView.text substringToIndex:BOOKMARK_WORD_LIMIT];          }      }  
0 0
原创粉丝点击