IOS电话号码输入

时间:2014-09-05 19:22:49

标签: ios objective-c

有没有办法轻松自动格式化电话号码?我希望让我的用户只输入数字并将其格式化为(###) - ### - ####,或者至少将我的键盘锁定到电话簿。该应用程序供iPad内部使用,所以我知道在属性检查器中将键盘设置为数字键盘不会做任何事情。

我有时间限制,所以最有效的建议会很棒。我最初使用正则表达式来验证文本字段,但是不同的用户将数字与我最近的测试中的数字不同于公司中的一个小组。有些人把###。###。####,有些放### ### ####和一些放(###) - ### - ####所以这很烦人。

1 个答案:

答案 0 :(得分:0)

我使用了类似的东西并取得了良好的效果,格式为(###)### - #### x ### ...这可能不是最有效的过程,但是出于所有意图和目的,它的工作做得很好。

假设你有一个textField的属性/实例变量,以及一个BOOL来跟踪你是否想要格式化它,这就是我实现它的方法。当我为电话号码创建textField时,我会执行以下操作:

self.textField.keyboardType = UIKeyboardTypeNumberPad;
self.textField.delegate = self;
[self.textField addTarget:self action:@selector(formatPhoneNumber) forControlEvents:UIControlEventEditingChanged];

以上使键盘成为数字键盘,每次更改值时我们调用formatPhoneNumber,同时确保实现UITextFieldDelegateProtocol并将自己设置为字段的委托。

然后,我们实现了shouldChangeCharactersInRange方法,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // this is what the textField's string will be after changing the characters
    NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    // flag that we should attempt to format the phone number ONLY if they are adding
    // characters, otherwise if they are deleting characters we simply want to allow
    // them to delete them and not format them
    _shouldAttemptFormat = resultString.length > self.textField.text.length;

    return YES;
}

格式电话号码方法只会尝试格式化数字,如果我们确定应该在shouldChangeCharactersInRange方法中,将当前文本分解为有用的电话号码片段,如(555)444-2222 - > 5554442222,然后很好地格式化:

- (void)formatPhoneNumber {
    // this value is determined when textField shouldChangeCharactersInRange is called on a phone
    // number cell - if a user is deleting characters we don't want to try to format it, otherwise
    // using the current logic below certain deletions will have no effect
    if (!_shouldAttemptFormat) {
        return;
    }

    // here we are leveraging some of the objective-c NSString functions to help parse and modify
    // the phone number... first we strip anything that's not a number from the textfield, and then
    // depending on the current value we append formatting characters to make it pretty
    NSString *currentString = self.textField.text;
    NSString *strippedValue = [currentString stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, currentString.length)];

    NSString *formattedString;
    if (strippedValue.length == 0) {
        formattedString = @"";
    }
    else if (strippedValue.length < 3) {
        formattedString = [NSString stringWithFormat:@"(%@", strippedValue];
    }
    else if (strippedValue.length == 3) {
        formattedString = [NSString stringWithFormat:@"(%@) ", strippedValue];
    }
    else if (strippedValue.length < 6) {
        formattedString = [NSString stringWithFormat:@"(%@) %@", [strippedValue substringToIndex:3], [strippedValue substringFromIndex:3]];
    }
    else if (strippedValue.length == 6) {
        formattedString = [NSString stringWithFormat:@"(%@) %@-", [strippedValue substringToIndex:3], [strippedValue substringFromIndex:3]];
    }
    else if (strippedValue.length <= 10) {
        formattedString = [NSString stringWithFormat:@"(%@) %@-%@", [strippedValue substringToIndex:3], [strippedValue substringWithRange:NSMakeRange(3, 3)], [strippedValue substringFromIndex:6]];
    }
    else if (strippedValue.length >= 11) {
        formattedString = [NSString stringWithFormat:@"(%@) %@-%@ x%@", [strippedValue substringToIndex:3], [strippedValue substringWithRange:NSMakeRange(3, 3)], [strippedValue substringWithRange:NSMakeRange(6, 4)], [strippedValue substringFromIndex:10]];
    }

    self.textField.text = formattedString;
}

以下是现在正在播放的视频:

https://www.dropbox.com/s/xq9vb9p62oamova/phone.mov?dl=0

如果你想引用更多与此相关的代码,我在处理iOS表单的开源项目中使用类似的东西。第一个链接是带有演示的gif,所以你可以看到它的实际效果。

https://raw.githubusercontent.com/mamaral/MAFormViewController/master/Screenshots/form_demo.gif https://github.com/mamaral/MAFormViewController

相关问题