2010-01-27 122 views
0

在我的iPhone應用程序中,我有一個textfield接受電話號碼。我需要以美國電話號碼格式顯示號碼 。這就像(000)000-0000。通常喜歡iPhone聯繫電話 號碼輸入。我怎樣才能做到這一點。任何想法將不勝感激。在iPhone應用程序中顯示美國格式的電話號碼

回答

0

由於用戶在文本字段中手動輸入電話號碼,因此可以使用UITextFieldDelegate方法textField:shouldChangeCharactersInRange:replacementString:通過在輸入3位數字後在第1位數字'''前添加'('''來動態更改輸入的電話號碼' - '6位數後。 (或)號碼輸入完成後,您可以更改顯示的格式是這樣的:

- (BOOL) textFieldShouldReturn:(UITextField *)textField { 
    //resign the keypad and check if 10 numeric digits entered... 
    NSRange range; 
    range.length = 3; 
    range.location = 3; 

textField.text = [NSString stringWithFormat:@"(%@)%@-%@", [textField.text substringToIndex:3], [textField.text substringWithRange:range], [textField.text substringFromIndex:6]; 
} 
0
- (BOOL) textFieldShouldReturn:(UITextField *)textField { 
    //resign the keypad and check if 10 numeric digits entered... 
    NSRange range; 
    range.length = 3; 
    range.location = 3; 

textField.text = [NSString stringWithFormat:@"(%@) %@-%@", [textField.text substringToIndex:3], [textField.text substringWithRange:range], [textField.text substringFromIndex:6]; 
} 
相關問題