-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL shouldInput = YES;
NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
NSString * newString = [NSString stringWithFormat:@"%@%@",textField.text,string];
NSNumber * number = [nf numberFromString:newString];
if (number)
shouldInput = YES;
else
shouldInput = NO;
if ([string isEqualToString:@"-"] || !string.length) {
shouldInput = YES;
}
return shouldInput;
}
转载于:https://www.cnblogs.com/shujucn/p/7481452.html