#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *toString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (toString.length > 0) {
// 保留规则: 小数点前9位,小数点后4位
NSString *stringRegex = @"(\\+)?(([0]|(0[.]\\d{0,4}))|([1-9]\\d{0,8}(([.]\\d{0,4})?)))?";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
BOOL flag = [predicate evaluateWithObject:toString];
if (!flag) {
return NO;
}
}
return YES;
}
iOS UITextField限制小数输入位数
最新推荐文章于 2019-09-27 09:46:36 发布