iOS点滴记录
2018-07-19 本文已影响67人
荔枝lizhi_iOS程序猿
1.只能输入固定的字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL res = [QKValidateHelper validInputWithFixCharacter:@"0123456789" inputString:string];
if (!res) {
[MBProgressHUD showFlashTextInWindow:@"只能输入数字"];
return NO;
}
return YES;
}
/** 验证输入只能为固定的字符串 基本方法
比如只能输入 "0123456789"
*/
+(BOOL)validInputWithFixCharacter:(NSString *)fixCharacter inputString:(NSString *)inputString{
BOOL result = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:fixCharacter];
int i = 0;
while (i < inputString.length) {
NSString * string = [inputString substringWithRange:NSMakeRange(i, 1)];
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
if (range.length == 0) {
result = NO;
break;
}
i++;
}
return result;
}