textField:shouldChangeCharactersInRange:replacementString: 限制字数,替换字符串
来源:互联网 发布:淘宝模特兼职的骗局 编辑:程序博客网 时间:2024/06/06 01:36
主要是深入理解代理方法textField:shouldChangeCharactersInRange:replacementString:的使用。
转自http://www.cnblogs.com/Clin/p/3413146.html
如果要限制UITextField输入长度最长不超过kMaxLength,那么需要实现做以下操作:
1、实现UITextFieldDelegate协议;
2、实现textField:shouldChangeCharactersInRange:replacementString:方法;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSInteger strLength = textField.text.length - range.length + string.length;
return (strLength <= kMaxLength);
}
方法- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
功能:
把textField中位置为range的字符串替换为string字符串;
此函数在textField内容被修改时调用;
返回值:
YES,表示修改生效;NO,表示不做修改,textField的内容不变。
参数说明:
textField:响应UITextFieldDelegate协议的UITextField控件。
range: UITextField控件中光标选中的字符串,即被替换的字符串;
range.length为0时,表示在位置range.location插入string。
string: 替换字符串;
string.length为0时,表示删除。
另外:http://www.tuicool.com/articles/yyQRnq
iOS6下UITextField退格变清空问题的解决方法
- textField:shouldChangeCharactersInRange:replacementString: 限制字数,替换字符串
- textField:shouldChangeCharactersInRange:replacementString:
- 深入理解iOS API系列(一) textField:shouldChangeCharactersInRange:replacementString:
- 深入理解iOS API系列(一) textField:shouldChangeCharactersInRange:replacementString:
- 深入理解iOS API系列(一) textField:shouldChangeCharactersInRange:replacementString:
- textfield 限制字数
- textfield 限制字数
- TextField、TextView限制字数
- UITextField切换中文输入,选中所输入的中文时,iOS7不会调用textField:shouldChangeCharactersInRange:replacementString协议方法。
- UITextField切换中文输入,选中所输入的中文时,iOS7不会调用textField:shouldChangeCharactersInRange:replacementString协议方法。
- textField和textView字数限制
- textField和textView字数限制
- 实现textfield的字数限制问题
- textView和textField 限制文字输入字数
- iOS开发之textField限制输入字数
- textfield之shouldChangeCharactersInRange函数探究
- 玄宇说:JQ实现限制字数替换“…”
- Extjs中TextField的自动缩放和字数限制示例
- 4.10 从头学Android之多媒体--使用MediaPlayer和SurfaceView播放视频
- 1103. Integer Factorization (30)
- Web前端开发-3
- Perl正则表达式总结_1
- Android--Android四大组件(一)(Android studio)
- textField:shouldChangeCharactersInRange:replacementString: 限制字数,替换字符串
- Python hashmap
- Spring4+Hibernate5:报错Cannot unwrap to requested type [javax.sql.DataSource]
- 关于苹果PUSH(APNS)机制
- Grunt学习——简化静态资源
- android布局优化
- python 切片
- 【hdu1712】ACboy needs your help——分组背包
- windows录音程序原理