Pregunta

En mi aplicación para el iPhone Tengo un textfield para aceptar el número de teléfono. Necesito para mostrar el número en el formato de número de teléfono de Estados Unidos. Eso es como (000) 000-0000. Por lo general como el iPhone Teléfono del contacto la introducción de números. Cómo puedo hacer esto. Cualquier idea será muy apreciada.

No hay solución correcta

Otros consejos

Para formatear auto-, he utilizado addTarget en combinación con mi que href="https://stackoverflow.com/users/189804/adam-eberbach"> Adam amablemente se hace referencia. La aplicación se describe aquí .

Usted recibirá el formateador número de teléfono desde

http: // el-perdido- beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html

No se olvide de utilizar la clase PhoneNumberFormatter. Esta clase también se Availabel en ese blog

Dado que el usuario está introduciendo el número de teléfono manualmente en el campo de texto, puede utilizar el textField:shouldChangeCharactersInRange:replacementString: UITextFieldDelegate método para cambiar dinámicamente el número de teléfono introducido añadiendo '(' antes de la primera dígito ')' después se introducen 3 dígitos '- 'después de 6 dígitos. (O) después de la entrada número se hace, se puede cambiar el formato que se muestra como esto:

- (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];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top