iOS限制输入框输入多个标点符号以及小数点保留后两位
#pragma mark - UITextField Delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// 判断是否已经存在小数点
if ([string isEqualToString:@"."] && [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
textField.text = @"";
return NO;
} else if ([string isEqualToString:@"."] && [textField.text containsString:@"."]) {
return NO;
}
NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
[futureString insertString:string atIndex:range.location];
NSInteger flag=0;
const NSInteger limited = 2; // 小数点 限制输入两位
for (NSInteger i = futureString.length-1; i>=0; i--) {
if ([futureString characterAtIndex:i] == '.') {
if (flag > limited) return NO;
break;
}
flag++;
}
return YES;
}