关于iOS 键盘输入限制(只能输入字母,数字)
#define NUM @"0123456789"
#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
关于iOS 键盘输入限制(只能输入汉字,字母,数字)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([self isInputRuleAndNumber:string] || [string isEqualToString:@""]) {
return YES;
}
return NO;
}
- (BOOL)isInputRuleAndNumber:(NSString *)str {
NSString *other = @"➋➌➍➎➏➐➑➒"; //九宫格的输入值
if ([other rangeOfString:str].location != NSNotFound) {
return YES;
}
NSString *pattern = @"[a-zA-Z\u4E00-\u9FA5\\u0030-\\u0039]+";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:str];
return isMatch;
}