iOS两位小数终极判断

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if (string.length == 0) {
        return YES;
    }

    BOOL isHaveDian   = NO;
    BOOL willHaveDian = NO;

    // 判断是否有小数点
    if ([textField.text containsString:@"."]) {
        isHaveDian = YES;
    } else {
        isHaveDian = NO;
    }
    
    if ([string containsString:@"."]) {
        willHaveDian = YES;
    } else {
        willHaveDian = NO;
    }
    
    if (string.length > 0) {
        
        if (string.length > 1) {
            NSString *regex = @"^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"; // 小数
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            BOOL isCode = [pred evaluateWithObject:string];
            if (!isCode) {
                MYLog(@"正则-您输入的格式不正确");
                return NO;
            }
        }
    
        // 当前输入的字符
        unichar single = [string characterAtIndex:0];
        
        // 不能输入.0~9以外的字符
        if (!((single >= '0' && single <= '9') || single == '.')){
            MYLog(@"您输入的格式不正确");
            return NO;
        }
        
        // 只能有一个小数点
        if (isHaveDian && single == '.') {
            MYLog(@"只能输入一个小数点");
            return NO;
        }
        
        if (isHaveDian && willHaveDian) {
            MYLog(@"只能输入一个小数点");
            return NO;
        }
        
        // 如果第一位是.则前面加上0
        if ((textField.text.length == 0) && (single == '.')) {
            textField.text = @"0";
        }
        
        // 如果第一位是0则后面必须输入.
        if ([textField.text hasPrefix:@"0"]) {
            if (textField.text.length > 1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1, 1)];
                if (![secondStr isEqualToString:@"."]) {
                    NSLog(@"第二个字符必须是小数点");
                    return NO;
                }
            } else {
                if (![string isEqualToString:@"."]) {
                    NSLog(@"第二个字符必须是小数点");
                    return NO;
                }
            }
        }
        
        NSInteger pointNum = 2;
        
        // 小数点后最多能输入两位
        if (isHaveDian || willHaveDian) {
            NSString *tobeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
            NSRange ran = [tobeString rangeOfString:@"."];
            if ((tobeString.length - ran.location - 1) > pointNum) {
                
                NSRange r = NSMakeRange(ran.location + 1, tobeString.length - ran.location - 1);
                
                NSString *frontString  = [tobeString substringToIndex:r.location];
                NSString *behindString = [[tobeString substringWithRange:r] substringToIndex:pointNum];
                
                NSString *fianlString = [NSString stringWithFormat:@"%@%@",frontString,behindString];
            
                textField.text = fianlString;
                NSLog(@"只能输入二两位小数");
                return NO;
            }
        }
    }

    return YES;
}

你可能感兴趣的:(iOS两位小数终极判断)