引入
在IOS实际开发中有些UITextField需要限制输入内容,如账号、密码等,下面简单介绍其简单的实现方法,如下: |
代码实现
//定义宏(限制输入内容)
#define kAlphaNum @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
#define kAlpha @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
#define kNumbers @"0123456789"
#define kNumbersPeriod @"0123456789."
//定义UITextFiled的代理方法:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSCharacterSet *cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
NSString *filtered =
[[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basic = [string isEqualToString:filtered];
return basic;
}