关于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
- 关于iOS textfield 在限制输入后无法退格的问题
- ios textfield 输入限制
- iOS限制TextField可输入的长度
- iOS - TextField的一些输入限制
- iOS如何限制textField的输入格式
- iOS textfield限制输入的最大长度
- iOS--如何限制textField输入时小数点后需要的个数
- iOS TextField电话号码输入限制
- IOS textField 输入个数限制
- iOS 怎么限制textView和textField的输入字数
- 限制TextField输入的内容
- Flash的输入框TextField无法输入中文的问题
- iOS 追踪限制textfield输入位数
- ios textfield限制中文输入长度
- iOS限制textfield输入两位小数
- iOS开发之textField限制输入字数
- iOS中textfield有效限制输入长度
- iOS 限制TextField输入长度(标准)
- Azure VIP,DIP,PIP的区别
- web后端开发-瞎聊
- Android Studio报错“Can't use Subversion command line client: svn”的解决
- python os模块功能和方法总结
- 03 Linux内核中几个重要数据结构
- 关于iOS textfield 在限制输入后无法退格的问题
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 一个简单的Spring MVC配置
- 深入理解AsyncTask
- 一个可视频语音的软件原型
- 安卓设计模式之观察者模式
- 欢迎使用CSDN-markdown编辑器
- 继承自NSObject的不常用又很有用的函数(2)
- 贝叶斯定律