iOS金额输入限制

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length ==10) {
        if ([string isEqualToString:@""]) {
            return YES;
        }
        return NO;
    }
    if (textField == self.moneyField) {
        if ([textField.text rangeOfString:@"."].location==NSNotFound) {
            _isContainsPoint = NO;
        }
        if ([textField.text rangeOfString:@"0"].location==NSNotFound) {
            _isFirstZero = NO;
        }
       
        if ([string length]>0){
            unichar single=[string characterAtIndex:0];//当前输入的字符
            if ((single >='0' && single<='9') || single=='.')//数据格式正确
            {
               
                if([textField.text length]==0){
                    if(single == '.'){
                        textField.text = @"0";
                        _isContainsPoint = YES;
                        //首字母不能为小数点
                        return YES;
                    }
                    if (single == '0') {
                        _isFirstZero = YES;
                        return YES;
                    }
                }
               
                if (single=='.'){
                    if(!_isContainsPoint)//text中还没有小数点
                    {
                        _isContainsPoint=YES;
                        return YES;
                    }else{
                        return NO;
                    }
                   
                }else if(single=='0'){
                    if ((_isFirstZero&&_isContainsPoint)||(!_isFirstZero&&_isContainsPoint)) {
                        //首位有0有.(0.01)或首位没0有.(10200.00)可输入两位数的0
                        if([textField.text isEqualToString:@"0.0"]){
                            return NO;
                        }
                        //前方不可插入0  00.11返回NO
                        if ([string isEqualToString:@"0"]) {
                            if (range.location == 0) {
                                return NO;
                            }else if (_isFirstZero&&_isContainsPoint){
                                if (range.location == 1) {
                                    return NO;
                                }
                            }
                        }
                       
                        NSRange ran=[textField.text rangeOfString:@"."];
                        int tt=(int)(range.location-ran.location);
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }else if (_isFirstZero&&!_isContainsPoint){
                        //首位有0没.不能再输入0
                        return NO;
                    }else{
                        return YES;
                    }
                }else{
                    if (_isContainsPoint){
                        //存在小数点,保留两位小数
                        NSRange ran=[textField.text rangeOfString:@"."];
                        int tt= (int)(range.location-ran.location);
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }else if(_isFirstZero&&!_isContainsPoint){
                        //首位有0没点
                        return NO;
                    }else{
                        return YES;
                    }
                }
            }else{
                //输入的数据格式不正确
                return NO;
            }
        }else{
            return YES;
        }
    }
    return YES;
}

你可能感兴趣的:(iOS金额输入限制)