iOS 中UITextField金额输入限制(类似纷享销客的年度
2017-06-20 本文已影响705人
村里竹竿
金额输入限制(小数点后两位+最多不超过10亿)
代码:
初始化
_moneyTextField = [[UITextField alloc] init];
_moneyTextField.font = [UIFont systemFontOfSize:14];
_moneyTextField.placeholder = @"请填写目标";
_moneyTextField.textColor = UIColorHex(5C5C5C);
_moneyTextField.borderStyle = UITextBorderStyleNone;
_moneyTextField.textAlignment = NSTextAlignmentLeft;
_moneyTextField.keyboardType = UIKeyboardTypeDecimalPad;
[self.contentView addSubview:_moneyTextField];
_moneyTextField.delegate = self;
[_moneyTextField addTarget:self action:@selector(textfieldTextDidChange:) forControlEvents:UIControlEventEditingChanged];
[_moneyTextField addTarget:self action:@selector(textfieldEditingDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
[_moneyTextField addTarget:self action:@selector(textfieldEditingDidEnd:) forControlEvents:UIControlEventEditingDidEnd];
/**
金额的判断,不超过十亿+小数点后两位
*/
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == _moneyTextField) {
NSScanner *scanner = [NSScanner scannerWithString:string];
NSCharacterSet *numbers;
NSRange pointRange = [textField.text rangeOfString:@"."];
if ((pointRange.length > 0) && (pointRange.location < range.location || pointRange.location > range.location + range.length)) {
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
} else {
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
}
if ([textField.text isEqualToString:@""] && [string isEqualToString:@"."] ) {
return NO;
}
short remain = 2; //默认保留2位小数
NSString *tempStr = [textField.text stringByAppendingString:string]; // 原来的字符+当前输入的字符
NSUInteger strlen = [tempStr length];
if(pointRange.length > 0 && pointRange.location > 0) { //判断输入框内是否含有“.”。
if([string isEqualToString:@"."]) { //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。
return NO;
}
if(strlen > 0 && (strlen - pointRange.location) > remain+1) { //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。
return NO;
}
}
NSRange zeroRange = [textField.text rangeOfString:@"0"];
if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。
textField.text = string;
return NO;
}else{
if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。
if([string isEqualToString:@"0"]){
return NO;
}
}
}
}
NSString *buffer;
if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) )
{
return NO;
}
// 大于10亿的时候不能再编辑
if ([tempStr longLongValue] >= 1000000000) {
return NO;
}
}
return YES;
}
- (void)textfieldEditingDidBegin:(UITextField *)textField
{
if ([textField.text containsString:@"元"]) {
textField.text = [textField.text stringByReplacingOccurrencesOfString:@"元" withString:@""];
}
if ([textField.text containsString:@","]) {
textField.text = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
}
}
- (void)textfieldEditingDidEnd:(UITextField *)textField
{
textField.text = [NSString stringWithFormat:@"%@元", [NSString balanceFormatFromStr:[NSString stringWithFormat:@"%.2f", [textField.text doubleValue]]]];
}
用到的NSString的分类
/**
* 格式化每三位带逗号的数据
* @param string 余额数据字符串
* @return 带逗号余额
*/
+ (NSString *)balanceFormatFromStr:(NSString*)string
{
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumberFormatter *numFormatter2 = [[NSNumberFormatter alloc] init];
[numFormatter2 setNumberStyle:NSNumberFormatterNoStyle];
NSNumber *num = [numFormatter2 numberFromString:string];
NSString *tempStr = [numFormatter stringFromNumber:num];
NSString *balanceStr = [tempStr substringFromIndex:1];
if ([tempStr hasPrefix:@"-"]) {
balanceStr = [NSString stringWithFormat:@"-%@",[tempStr substringFromIndex:2]];
}
return balanceStr;
}