textField限制输入整数0-100

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSString *regexStr = @"^\\d{0,3}$";
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self matches %@", regexStr];
    if([predicate evaluateWithObject:text] && [text integerValue] <= 100){
        return YES;
    }
    return NO;
    
}

猜你喜欢

转载自my.oschina.net/u/2519763/blog/1549801