对输入框输入的字符进行判断,检查输入类型是不是数字计算的类型
//对输入的字符进行判断
static NSString *numbers = @"0123456789";
static NSString *numbersPeriod = @"0123467890.";
static NSString *numbersComma = @"0123456789,";
if (range.length > 0 && [string length] == 0) {//删除内容
// enable delete
return YES;
}
//检查符号是不是在第一位
NSString *symbol = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
if (range.location == 0 && [string isEqualToString:symbol]) {
// decimalseparator should not be first
return NO;
}
//判断第二位的不是小数点,且第一位的数字是0
if(range.location == 1){
if ([textField.text isEqualToString:@"0"] && ![string isEqualToString:@"."]){
return NO;
}
}
NSCharacterSet *characterSet;
NSRange separatorRange = [textField.text rangeOfString:symbol];
if (separatorRange.location == NSNotFound) {
if ([symbol isEqualToString:@"."]) {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersPeriod] invertedSet];
}
else {
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbersComma] invertedSet];
}
}
else {
// allow 2 characters after the decimal separator
if (range.location > (separatorRange.location + 2)) {
return NO;
}
characterSet = [[NSCharacterSet characterSetWithCharactersInString:numbers] invertedSet];
}
if ([[string stringByTrimmingCharactersInSet:characterSet] length] == 0) {
return NO;
}
(๑•̀ㅂ•́)و✧ φ(≧ω≦*)♪
快快乐乐才是真,拼拼博博才会赢。
(๑•̀ㅂ•́)و✧ φ(≧ω≦*)♪