IOS中UITextView或UITextField字数限制的实现
UITextView或UITextField字数限制,输入时的限制,复制粘贴时的限制
字数限制有三种方法
在代理方法
“- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string”或
“- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text”中实现两种方法
方法1(只能在输入时限制,复制粘贴时无法限制)
方法2(输入及复制粘贴时均可限制)
在代理方法
“- (void)textViewDidChange:(UITextView *)textView”中实现一种方法
方法3(复制粘贴时均可限制)
注意:
“NSString *temp = [textField.text stringByReplacingCharactersInRange:range withString:string];”为字符范围替换为指定的字符串,返回新的字符串。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!