/// 数字输入限制(integerLength整数位数0时无限制;greater整数首位是否大于0;decimal是否带小数,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;
}