钱的限制输入条件
1.可以输入小数点后N位
2.第一位是0,第二位不能是0
3.不能以.开始
方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string { NSCharacterSet *characterSet; NSUInteger dotLocation = [textField.text rangeOfString:@"."].location; if ([textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 0 && [string isEqualToString:@"."]) { textField.text = @"0."; return NO; } if (dotLocation == NSNotFound && range.location != 0) { NSMutableCharacterSet *mutableCharacterSet = [NSMutableCharacterSet decimalDigitCharacterSet]; [mutableCharacterSet addCharactersInString:@"."]; characterSet = [mutableCharacterSet invertedSet]; }else { characterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; } NSString *filtered = [[string componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""]; BOOL basicTest = [string isEqualToString:filtered]; if(!basicTest) { return NO; } if (dotLocation != NSNotFound && range.location > dotLocation + 2) { return NO; } return YES; } |
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string { NSMutableString * futureString = [NSMutableString stringWithString:textField.text]; [futureString insertString:string atIndex:range.location]; NSInteger flag=0; const NSInteger limited = 1; for (int i = futureString.length-1; i>=0; i--) { if([futureString characterAtIndex:i] == '.') { if (flag > limited) { return NO; } break; } flag++; } return YES; } |
方法三
代码忘了在哪看见的了,之前随手收藏的,提供一个思路吧,observeMoney是UITextFiled category中的一个方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
- (void)observeMoney { if (!self.isMoney) { return; } NSString *allText = self.text; NSString *newText= allText; if (self.correctText) { NSRange oldTextRange = [allText rangeOfString:self.correctText]; newText = [allText substringFromIndex:oldTextRange.length]; } NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."]invertedSet]; NSString *filtered = [[newText componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""]; if (filtered.length == 0) { if ([newText isEqualToString:@""]) {//inputed backSpace self.correctText = newText; } self.text = self.correctText; return; } if ([self.correctText rangeOfString:@"."].length) { if ([newText isEqualToString:@"."]) {//only one '.' self.text = self.correctText; return; } //2 charactors limited after '.' NSArray *array = [self.correctText componentsSeparatedByString:@"."]; if (array.count == 2) { if ([array[1] length] >= 2) { NSArray *newStringArray = [newText componentsSeparatedByString:@"."]; if (newStringArray.count == 2 && [newStringArray[1] length] == 1) {//inputed backSpace self.correctText= newText; } self.text = self.correctText; return; } } } else if (self.correctText.length == 1) { if ([newText isEqualToString:@"0"] && [self.correctText isEqualToString:@"0"]) { self.text = self.correctText; return; } } if (self.correctText.length == 0 && [newText isEqualToString:@"."]) {//"."->"0." self.text = self.correctText = @"0."; return; } if (self.correctText.length == 1 && [self.correctText isEqualToString:@"0"]) { if (![newText isEqualToString:@"."] && [newText intValue] > 0) {//'0'->other numbers self.text = self.correctText = newText; return; } } self.correctText = allText; } |
转载请注明:天狐博客 » iOS开发之TextField钱以及小数点的实时限制输入