UITextField数字输入限制

/// 数字输入限制(integerLength整数位数0时无限制;greater整数首位是否大于0decimal是否带小数,decimalLength小数位数)

- (BOOL)limitNumberShouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string integer:(NSInteger)integerLength greaterThanZero:(BOOL)greater decimalPoint:(BOOL)decimal decimalDigits:(NSInteger)decimalLength

{

    if ([self isFirstResponder])

    {

        NSCharacterSet *withoutZeroNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"123456789"];

        NSCharacterSet *withZeroNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

        NSCharacterSet *limitSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];

        NSString *decimalString = @".";

        NSString *zeroString = @"0";

        

        NSString *tempStr = [self.text stringByReplacingCharactersInRange:range withString:string];

        NSLog(@"text(integerLength=%d length = %ld) %@", integerLength, tempStr.length, tempStr);

        

        if (1 < tempStr.length)

        {

            NSString *firstString = [tempStr substringWithRange:NSMakeRange(0, 1)];

            if ([zeroString isEqualToString:firstString])

            {

                // 第一个是0时,且第二个非小数点时,自动去掉第一个0

                NSString *secondString = [tempStr substringWithRange:NSMakeRange(1, 1)];

                if ([decimalString isEqualToString:secondString])

                {

                    // 带小数

                    if (decimal)

                    {

                        NSRange decimalRange = [tempStr rangeOfString:decimalString];

                        if (decimalRange.location != NSNotFound)

                        {

                            // 小数点后decimalLength

                            NSInteger decimal = (decimalRange.location + decimalRange.length + decimalLength + 1);

                            if (decimal <= tempStr.length)

                            {

                                return NO;

                            }

                        }

                    }

                }

                else

                {

                    self.text = [tempStr substringFromIndex:1];

                    return NO;

                }

            }

            else

            {

                // 第一个非0

                

                for (int i = 0; i < tempStr.length; i++)

                {

                    NSString *subString = [tempStr substringWithRange:NSMakeRange(i, 1)];

                    

                    // 只能输入数字和小数点

                    NSRange numberRange = [subString rangeOfCharacterFromSet:(decimal ? limitSet : withZeroNumberSet)];

                    if (numberRange.location == NSNotFound)

                    {

                        return NO;

                        break;

                    }

                    

                    if (decimal)

                    {

                        // 只能有一个小数点

                        NSArray *array = [tempStr componentsSeparatedByString:decimalString];

                        if (3 <= array.count)

                        {

                            return NO;

                            break;

                        }

                    }

                }


                // 带小数

                if (decimal)

                {

                    NSRange decimalRange = [tempStr rangeOfString:decimalString];

                    if (decimalRange.location != NSNotFound)

                    {

                        // 小数点后decimalLength

                        NSInteger decimal = (decimalRange.location + decimalRange.length + decimalLength + 1);

                        if (decimal <= tempStr.length)

                        {

                            return NO;

                        }

                    }

                }

                

                // 整数长度,0时整数输入无限制

                if (integerLength != 0)

                {

                    NSRange decimalRange = [tempStr rangeOfString:decimalString];

                    NSString *subString = tempStr;

                    if (decimalRange.location != NSNotFound)

                    {

                        subString = [tempStr substringToIndex:decimalRange.location];

                    }

                    

                    NSInteger integer = (integerLength + 1);

                    if (integer <= subString.length)

                    {

                        return NO;

                    }

                }

            }

        }

        else if (1 == tempStr.length)

        {

            // 首个输入限制,只能输入1~9,或只有输入0~9

            NSRange numberRange = [tempStr rangeOfCharacterFromSet:(greater ? withoutZeroNumberSet : withZeroNumberSet)];

            if (decimal && !greater)

            {

                // 带小数点时,且首个输入非大于0时,允许首个输入小数点,并默认格式为"0.xxx"

                numberRange = [tempStr rangeOfCharacterFromSet:limitSet];

                if ([decimalString isEqualToString:tempStr])

                {

                    self.text = [NSString stringWithFormat:@"%@%@", zeroString, decimalString];

                    return NO;

                }

            }

            

            if (numberRange.location == NSNotFound)

            {

                return NO;

            }

        }

    }

    return YES;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值