0

我的应用程序上有一个UITextField接收号码的 a。

我希望这个数字以特定格式表示,如下所示:(62) 1234 1234

问题是:我已经控制了方法-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string,但我不知道如何制作这个“面具”

目前我正在这样做:

string = [textField.text stringByReplacingCharactersInRange:range withString:string];
string = [[string componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:string];
NSLog(@"%@", myNumber);

在这段代码的末尾,我有两件事:

  1. string :我的文本的字符串表示形式(例如6212341234作为 a NSString
  2. myNumber :此字符串的编号(例如6212341234作为 a NSNumber

我怎样才能创建这个字符串:(62) 1234 1234基于myNumberstring

编辑

回答 rdelmar 问题:

该数字可以有 10 或 11 个数字,我打算像这样分开它们: (62) 1234 1234(62) 12345 1234

4

2 回答 2

1

你可以用NSRegularExpression...

- (NSString *) formattedString:(NSString *)inputString
{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:([0-9]{2})([0-9]{5})([0-9]{4}))|(?:([0-9]{0,2}+)([0-9]{0,4}+)([0-9]{0,4}+))" options:0 error:&error];
    NSArray *components = [NSArray array];
    NSArray *matches = [regex matchesInString:inputString options:0 range:NSMakeRange(0, [inputString length])];
    NSTextCheckingResult *match = [matches objectAtIndex:0];
    for ( NSUInteger i = 1; i <= 3; ++i )
    {
        NSRange range = [match rangeAtIndex:i];
        if ( NSNotFound == range.location )
        {
            break;
        }
        components = [components arrayByAddingObject:[inputString substringWithRange:range]];
    }
    if ( 0 == [components count] )
    {
        for ( NSUInteger i = 4; i <= 6; ++i )
        {
            NSRange range = [match rangeAtIndex:i];
            components = [components arrayByAddingObject:[inputString substringWithRange:range]];
        }
    }
    return [NSString stringWithFormat:@"(%@) %@ %@",
            [components objectAtIndex:0],
            [components objectAtIndex:1],
            [components objectAtIndex:2]];
}


- (void) testFormatting
{
    NSLog(@"%@", [self formattedString:@"62123451234"]);
    NSLog(@"%@", [self formattedString:@"6212341234"]);
    NSLog(@"%@", [self formattedString:@"621234123"]);
    NSLog(@"%@", [self formattedString:@"62123412"]);
    NSLog(@"%@", [self formattedString:@"6212341"]);
    NSLog(@"%@", [self formattedString:@"621234"]);
    NSLog(@"%@", [self formattedString:@"62123"]);
    NSLog(@"%@", [self formattedString:@"6212"]);
    NSLog(@"%@", [self formattedString:@"621"]);
    NSLog(@"%@", [self formattedString:@"62"]);
    NSLog(@"%@", [self formattedString:@"6"]);
}

调用该testFormatting方法的结果是:

(62) 12345 1234
(62) 1234 1234
(62) 1234 123
(62) 1234 12
(62) 1234 1
(62) 1234 
(62) 123 
(62) 12 
(62) 1 
(62)  
(6)  

这样,您可以将中间字符串传递给格式化程序,它会尝试做正确的事情。

于 2012-09-29T16:48:49.117 回答
0

你可以这样做(对于 10 位数字):

-(NSString *)formatString:(NSString *) input {
    NSString *part1 = [NSString stringWithFormat:@"(%@)",[input substringToIndex:2]];
    NSString *part2 = [input substringWithRange:NSMakeRange(2, 4)];
    NSString *part3 = [input substringWithRange:NSMakeRange(6, 4)];
    NSString *formatted = [NSString stringWithFormat:@"%@ %@ %@",part1,part2,part3];
    return formatted;

}

要处理 10 位或 11 位数字,您必须放入 if 子句来测试长度,然后相应地更改 part2 和 part3 的范围。

编辑后:这样的事情怎么样。我认为这是可以做到的。它会在您键入时将其分解为 (xx) xxxx xxxx,如果您键入另一个数字,它会将其分解为 (xx) xxxx xxxx。任何进一步的输入都将被忽略。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length == 0)
     textField.text = [NSString stringWithFormat:@"(%@",textField.text];

    if (textField.text.length == 3)
        textField.text = [NSString stringWithFormat:@"%@) ",textField.text];

    if (textField.text.length == 9)
        textField.text = [NSString stringWithFormat:@"%@ ",textField.text];

    if (textField.text.length == 14) {
        NSString *part1 = [textField.text substringToIndex:9];
        part1 = [part1 stringByAppendingString:[textField.text substringWithRange:NSMakeRange(10, 1)]];
        NSString *part2 = [textField.text substringWithRange:NSMakeRange(11, 3)];
        textField.text = [NSString stringWithFormat:@"%@ %@",part1,part2];
    }

    if (textField.text.length > 14)
        return NO;

    return YES;
}
于 2012-09-28T21:13:07.263 回答