【问题标题】:IOS phone number entry [duplicate]IOS电话号码输入[重复]
【发布时间】:2014-09-05 19:22:48
【问题描述】:

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

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

【问题讨论】:

标签: ios objective-c


【解决方案1】:

我使用了类似的东西,效果很好,格式是 (###) ###-#### 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;
}

format phone number 方法只会在我们确定应该在 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

【讨论】:

  • 太棒了!好东西。有没有找到删除的方法,格式中的)和-之后好像卡住了。
  • 真的吗?让我检查一下,它不应该,但我在这里复制粘贴并进行了一些编辑,所以我可能遗漏了一些东西。
  • 啊,是的,让我做一些补充......
  • 添加了更多解释和视频演示。对你有用吗?
  • 是的,你是最棒的!非常感谢
猜你喜欢
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 2017-08-25
相关资源
最近更新 更多