2014-09-05 72 views
0

有沒有辦法自動格式化電話號碼?我想讓我的用戶輸入號碼,並將其格式化爲(###) - ### - ####,或者至少將我的鍵盤鎖定在電話鍵盤上。該應用程序是在iPad上內部使用的,所以我知道在屬性檢查器中將鍵盤設置爲數字鍵盤不會執行任何操作。IOS電話號碼條目

我在時間限制,所以最有效的建議將是偉大的。我最初將用正則表達式來驗證文本字段,但不同的用戶將公司中的一個小組與我最近的測試不同的數字。一些放###。###。####,一些放### ### ####和一些放(###) - ### - ####所以這只是令人討厭。

+0

怎麼樣的國家代碼? + 1-123-456-7890也是一個有效的電話號碼。 – 2014-09-05 19:24:34

+0

號碼將只有123-456-7890。範圍不適用於國際號碼 – user3443430 2014-09-05 19:26:29

+0

另請參閱:[iPhone簡單電話號碼驗證](http://stackoverflow.com/q/7180847) – Caleb 2014-09-05 19:34:15

回答

0

我用過類似的東西,效果很好,格式是(###)### - #### x ### ...它可能不是最有效率的過程,但是對於所有的意圖和目的來說,這項工作非常好。

假設你有一個屬性/實例變量爲你的textField,以及一個BOOL來跟蹤你是否要允許它被格式化,這裏是我如何實現它。當我創建我的文本字段的電話號碼,我做到以下幾點:

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

+0

太棒了!好東西。你有沒有找到一種方法來刪除,它似乎卡住後)和 - 格式。 – user3443430 2014-09-05 20:06:51

+0

真的嗎?讓我檢查一下,它不應該,但我做了複製粘貼並在這裏做一些編輯,所以我可能會錯過一些東西。 – Mike 2014-09-05 20:08:12

+0

啊是的,讓我做一些補充.. – Mike 2014-09-05 20:13:53