說明: ^.*[\u4e00-\u9fa5].*$ 是否包含中文^[\u4E00-\u9FA5]+$ 是否全中文 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range repla ...
說明:
^.*[\u4e00-\u9fa5].*$ 是否包含中文
^[\u4E00-\u9FA5]+$ 是否全中文
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (![string isEqualToString:@""]) {
if ([self isChinese:string]) {
return NO;
}
}
return YES;
}
- (BOOL)isChinese:(NSString *)string {
NSString *regex = @"^[\u4E00-\u9FA5]+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:string];
return isMatch;
}