关于iOS textfield 在限制输入后无法退格的问题

来源:互联网 发布:淘宝造物节抽手机 编辑:程序博客网 时间:2024/05/21 19:23

 

通常项目当中要用到textfiled的限制字数输入的代理 


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    <span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;">if</span> (textField == self.titleField) {        <span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;">if</span> (textField.length > 20) 
<span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="white-space:pre"></span>return</span> NO;    }    <span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;">return</span> YES;}
 当输到第19个字符后想退格 因为

textField.length > 20 一直是大于20

所以无法输入也无法退格。

只要稍微加一个判断 :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  
<span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="white-space: pre;"></span>  if ([[textField.text stringByReplacingCharactersInRange:range withString:string] length] >20) {            [MBHelper showHUDViewWithText:@"只允许输入两位数字" withDur:1];            return NO;        }</span>    <span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;">return</span> YES;}
</pre>这样就可以了.</p><p><span style="color: rgb(47, 47, 47); font-family: 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 24.7273px;">或者<span style="color: rgb(47, 47, 47); font-family: 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 24.7273px;">把return No 去掉换成:</span></span></p><p><span style="color: rgb(47, 47, 47); font-family: 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 24.7273px;"></span><pre class="code-java" name="code" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 12.35px; padding: 0px; overflow: auto; font-family: 'Courier New', Courier, monospace; line-height: 1.3;">- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    <span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;">if</span> (textField == self.titleField) {        <span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;">if</span> (textField.length > 20) 
<span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="white-space: pre;"></span>textfield.text = [textfiled.text <span class="s1" style="font-size: 12.35px; line-height: 1.3; background-color: inherit;">substringToIndex</span><span class="s2" style="font-size: 12.35px; line-height: 1.3; background-color: inherit;">:20</span>];</span>    }    <span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;">return</span> YES;}

另外 textField 的代理用来过滤字数 会存在一个系统的Bug
系统代理无法监听用户点选键盘智能提醒的汉字,推荐使用 自定义监听事件来处理

[_titleTextField addTarget:self action:@selector(textfieldDidChange:) forControlEvents:UIControlEventEditingChanged];



但这个系统Bug 放在 UITextView 中 我还不知道怎么解决,有想法的可以交流一下哈。


0 0
原创粉丝点击