0

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

4

1 回答 1

0

这是我对这个问题的回答,我使用的是 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;

}
于 2016-06-11T11:06:44.613 回答