我的应用程序中有一个UITextField
,它会收到一个号码。
我希望这个数字以特定的格式表示,例如:(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);
在这段代码的最后我有两件事:
6212341234
为NSString
)6212341234
为NSNumber
)如何基于(62) 1234 1234
或myNumber
创建此字符串:string
?
修改
回应rdelmar问题:
这个号码可以有10个或11个号码,我打算将它们分开:
(62) 1234 1234
或(62) 12345 1234
答案 0 :(得分: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)
这样你可以将中间字符串传递给格式化程序,它会尝试做正确的事情。
答案 1 :(得分: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)xxxxx 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;
}