UItextField限制字符输入的个数
(2012-11-02 17:51:57) 标签: uitextfield字符输入的个数限制杂谈 | 分类: iPhone开发 |
方法一:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersIn
//按回车关闭键盘
//判断是不是删除键
//输入的字符个数大于10,则无法继续输入,返回NO表示禁止输入
}
方法二:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
int MAX_CHARS = 4;
NSMutableString *newText = [NSMutableString stringWithString:textField.text];
[newText replaceCharactersInRange:range withString:string];
return ([newText length] <= MAX_CHARS);
}
本文介绍两种在iOS应用中使用UITextField限制用户输入字符数量的方法。方法一通过监测文本字段内容长度实现,当达到预设的最大长度时阻止进一步输入。方法二则是在输入变化前检查新的文本长度是否超过设定的最大值。
2万+

被折叠的 条评论
为什么被折叠?



