1.只能输入数字和小数点
2.小数点前只能有一个0
3.小数点后两位
// MARK: -- 键盘代理
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:@""]) { // 删除操作
if ([textField.text isEqualToString:@"0"]) {
textField.text = @"";
}
}
// 输入小数点补0
if ([textField.text isEqualToString:@""] && [@"." isEqualToString:string]) {
textField.text = @"0.";
}
if ([textField.text isEqualToString:string] && [@"0" isEqualToString:string]) { // 前两位不能为00
return NO;
}else{ // 可以是小数点且只能是小数点
if (![textField.text isEqualToString:@"0"] || [string isEqualToString:@"."]) {
NSInteger dotLocation = [textField.text rangeOfString:@"."].location;
if (dotLocation != NSNotFound){
if ([string isEqualToString:@"."]){
return NO;
}
}
//判断小数点后最多两位
if (dotLocation != NSNotFound && range.location > dotLocation + 2) {
return NO;
}
}else{
return NO;
}
}
return YES;
}