【问题标题】:How to format UITextField with NSNumberFormatter?如何使用 NSNumberFormatter 格式化 UITextField?
【发布时间】:2016-06-11 11:06:44
【问题描述】:

希望这对其他人有所帮助,因为我还没有在网上看到任何类似的帖子显示如何使用 NSNumberFormatter 格式化文本字段,但同时将 UITextField 光标位置保持在它应该自然的位置.那些,因为在格式化之后,UITextField 内部的NSString 并将其设置回field 最终会将光标放在field 的末尾。 对于需要它的人来说,将它转换为 Swift 也会很好。

【问题讨论】:

    标签: objective-c swift uitextfield nsnumberformatter uitextposition


    【解决方案1】:

    这是我对这个问题的回答,我使用的是 UIKeyboardTypeNumberPad,但它也适用于 UIKeyboardTypeDecimalPad,如果使用其他键盘类型,请在使用下一个代码之前随意添加正则表达式:

    - (int)getCharOccurencies:(NSString *)character inString:(NSString *)string{
    
        NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[string length]];
        for (int i=0; i < [string length]; i++) {
            NSString *ichar  = [NSString stringWithFormat:@"%c", [string characterAtIndex:i]];
            [characters addObject:ichar];
        }
    
        int count = 0;
        for (NSString *ichar in characters) {
            if ([ichar isEqualToString:character]) {
                count++;
            }
        }
        return count;
    
    }
    
    - (void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
        UITextPosition *start = [input positionFromPosition:[input beginningOfDocument]
                                                     offset:range.location];
        UITextPosition *end = [input positionFromPosition:start
                                                   offset:range.length];
        [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
    }
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
        NSString *charUsedToFormat = @",";
    
        NSMutableString *textString = [NSMutableString stringWithString:[textField text]];
        [textField setTintColor:[UIColor darkGrayColor]];
    
        if ([string isEqualToString:charUsedToFormat]) {
            return NO;
        }
    
        if (range.location == 0 && [string isEqualToString:@"0"]) {
            return NO;
        }
    
        if ([[textString stringByReplacingCharactersInRange:range withString:string] length] == 0) {
            textField.text = @"";
            [self selectTextForInput:textField atRange:NSMakeRange(0, 0)];
            return NO;
        }
    
        NSString *replacebleString = [textString substringWithRange:range];
    
        if (string.length == 0 && [replacebleString isEqualToString:charUsedToFormat]) {
    
            NSRange newRange = NSMakeRange( range.location - 1, range.length);
            range = newRange;
            textString = [NSMutableString stringWithString:[textString stringByReplacingCharactersInRange:newRange withString:string]];
        }else{
             textString = [NSMutableString stringWithString:[textString stringByReplacingCharactersInRange:range withString:string]];
        }
    
    
        int commmaCountBefore = [self getCharOccurencies:charUsedToFormat inString:textString];
    
        textString = [NSMutableString stringWithString:[textString stringByReplacingOccurrencesOfString:charUsedToFormat withString:@""]];
        NSNumber *firstNumber = [NSNumber numberWithDouble:[textString doubleValue]];
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
        //just in case
        [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
        [formatter setGroupingSeparator:charUsedToFormat];
        [formatter setDecimalSeparator:@""];
        [formatter setMaximumFractionDigits:0];
        textString = [NSMutableString stringWithString:[formatter stringForObjectValue:firstNumber]];
        textField.text = textString;
    
        int commmaCountAfter = [self getCharOccurencies:charUsedToFormat inString:textString];
        int commaDif = commmaCountAfter - commmaCountBefore;
    
    
        int cursorPossition = (int)range.location + (int)string.length + commaDif;
    
        //set cursor position
        NSLog(@"cursorPossition: %d", cursorPossition);
    
    
        [self selectTextForInput:textField atRange:NSMakeRange(cursorPossition, 0)];
    
        return NO;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      相关资源
      最近更新 更多