Domanda

Nel mio iPhone app ho un textfield per accettare il numero di telefono. Ho bisogno di visualizzare il numero negli Stati Uniti formato Numero di telefono. Che è come (000) 000-0000. In genere come iPhone Telefono del contatto inserimento del numero. Come posso fare questo. Qualsiasi idea sarà molto apprezzato.

Nessuna soluzione corretta

Altri suggerimenti

Per auto-formattazione, ho usato addTarget in combinazione con il mio PhoneNumberFormatter che Adam gentilmente riferimento. L'implementazione è descritto qui .

Si otterrà il numero di telefono da formattatore

http: // il-stassi perdendo beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html

Non dimenticare di utilizzare la classe PhoneNumberFormatter. Questa classe è anche availabel in quel blog

Dato che l'utente sta entrando il numero di telefono manualmente nel campo di testo, è possibile utilizzare il textField:shouldChangeCharactersInRange:replacementString: metodo UITextFieldDelegate per modificare dinamicamente il numero di telefono inserito con l'aggiunta di '(' prima del 1 ° cifra ')' dopo 3 cifre sono entrato '- 'dopo 6 cifre. (O) dopo l'immissione del numero è fatto, è possibile modificare il formato visualizzato in questo modo:

- (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];
}
- (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];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top