限制UITextField大小 [英] Restrict UITextField size
本文介绍了限制UITextField大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何限制UITextField中的字符数?
How can I limit the number of characters in a UITextField?
如果我将其限制为10个字符,用户将无法输入超过10个字符。此外,我想阻止他输入一些特殊字符,如+,=等。
I.e if I limit it to 10 characters, the user will be unable to enter more than 10. Also I want to prevent him from entering some special characters like +, =, etc.
我该如何做?
推荐答案
这里是一个代码示例来限制UITextField的大小:
Here is a code sample to limit the size of a UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([[textField text] length] + [string length] - range.length > MAX_LENGTH) {
return NO;
} else {
return YES;
}
}
这篇关于限制UITextField大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文