关于iOS textfield 在限制输入后无法退格的问题
2016-06-03 14:45
501 查看
通常项目当中要用到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 中 我还不知道怎么解决,有想法的可以交流一下哈。
相关文章推荐
- ios 浏览带目录的pdf文件
- IOS 常用方法封装
- 【iOS开发】NSString字符串最全判空方法
- iOS5.1下emoji表情显示方框的解决办法
- [置顶] iOS系统相关参数(版本、机型设备等)
- iOS修改系统原声的返回按钮的点击事件
- 【iOS开发】字典(NSDictionary)和JSON字符串(NSString)之间互转
- iOS json转化为model
- iOS 上架- IPA打包上传遇到问题记录
- iOS 关于屏幕旋转shouldAutorotate
- iOS开发iOS7状态栏文字颜色修改
- Bugly IOS Crash 符号表生成使用记录
- iOS第三方资源
- iOS打包导出iPa四个选项意义
- iOS多级跳转,返回指定viewcontroller
- 如何设计一个 iOS 控件?(iOS 控件完全解析)--转载
- iOS 大杂烩
- PCH文件使用
- iOS8 之后tableview的delegate中新增加的代理方法,可以在滑动的时候添加多个按钮。
- iOS 8 Xcode6 设置Launch Image 启动图片