我用過類似的東西,效果很好,格式是(###)### - #### 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
怎麼樣的國家代碼? + 1-123-456-7890也是一個有效的電話號碼。 – 2014-09-05 19:24:34
號碼將只有123-456-7890。範圍不適用於國際號碼 – user3443430 2014-09-05 19:26:29
另請參閱:[iPhone簡單電話號碼驗證](http://stackoverflow.com/q/7180847) – Caleb 2014-09-05 19:34:15