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

本文介绍如何在iOS开发中使用UITextField的代理方法来实现输入字数限制,并解决了因系统BUG导致无法监听键盘智能提醒汉字的问题。

 

通常项目当中要用到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 中 我还不知道怎么解决,有想法的可以交流一下哈。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值