textFiled小数点问题

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField  == self.tyt_InputTextField){
    
        NSInteger length = 7;
        if (self.config.maxContentLength >0) {
            length = self.config.maxContentLength;
        }
    
        if (self.config) {
            if (textField.text.length == 0&&[string isEqualToString:@"."]) {
                textField.text = @"0";
            }
            if (self.config.isInt == YES) {
                if ([string isEqualToString:@"."]) {
                    return NO;
                }
            }
            if ([textField.text isEqualToString:@"0"]&&![string isEqualToString:@"."]&&range.location != 0) {
                return NO;
            }
            if ([textField.text rangeOfString:@"."].location != NSNotFound||[string isEqualToString:@"."]) {
                length = 11;
                if (self.config.maxContentLength>0) {
                    
                    length = self.config.maxContentLength  + 4;
                    if (self.config.isFloat == YES) {
                      length = self.config.maxContentLength  + 1 + 4;
                    }
                }
            }
            if ([self decimalPointTextFiled:textField shouldChangeCharactersInRange:range replacementString:string] == NO) {
                return NO;
            }
        }
       
        if (string.length == 0) return YES;
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
     
      
        if (existedLength - selectedLength + replaceLength > length){
            return NO;
        }
    }
    return YES;
}

- (BOOL)decimalPointTextFiled:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    //    判断当前是否有小数点
    if([textField.text rangeOfString:@"."].location!=NSNotFound){
        if ([string isEqualToString:@"."]){
            return NO;
        }
    }
//    首位不可重复输入0
    if ([textField.text isEqualToString:@"0"] && [string isEqualToString:@"0"])
        
    {
        return NO;
    }
     //拼出输入完成的str,判断str的长度大于等于“.”的位置+4,则返回false,此次插入string失败 ("379132.424",长度10,"."的位置6, 10>=6+4)

        NSMutableString *str = [[NSMutableString alloc] initWithString:textField.text];

        [str insertString:string atIndex:range.location];
    if (self.config.isFloat == YES&& self.config.maxContentLength == 2) {
        
        if (str.length >= [str rangeOfString:@"."].location + 3){

            return NO;

        }
    }else{
       if (str.length >= [str rangeOfString:@"."].location + 4){

            return NO;

        }
    }
        
 
    return YES;
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值