2011-09-04 12 views
157

E 'possibile modificare a livello di codice il tipo di tastiera di un UITextField in modo che qualcosa di simile sarebbe possibile:modificare a livello di UITextField Tipo tastiera

if(user is prompted for numeric input only) 
    [textField setKeyboardType: @"Number Pad"]; 

if(user is prompted for alphanumeric input) 
    [textField setKeyboardType: @"Default"]; 
+3

ti suggerirei di cambiare il termine 'doozy' in qualcosa che è più comunemente comprensibile ..tieni presente che SO è un sito internazionale e non uno nordamericano – abbood

risposta

344

c'è una proprietà keyboardType per un UITextField:

typedef enum { 
    UIKeyboardTypeDefault,    // Default type for the current input method. 
    UIKeyboardTypeASCIICapable,   // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active 
    UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation. 
    UIKeyboardTypeURL,     // A type optimized for URL entry (shows ./.com prominently). 
    UIKeyboardTypeNumberPad,    // A number pad (0-9). Suitable for PIN entry. 
    UIKeyboardTypePhonePad,    // A phone pad (1-9, *, 0, #, with letters under the numbers). 
    UIKeyboardTypeNamePhonePad,   // A type optimized for entering a person's name or phone number. 
    UIKeyboardTypeEmailAddress,   // A type optimized for multiple email address entry (shows space @ . prominently). 
    UIKeyboardTypeDecimalPad,    // A number pad including a decimal point 
    UIKeyboardTypeTwitter,    // Optimized for entering Twitter messages (shows # and @) 
    UIKeyboardTypeWebSearch,    // Optimized for URL and search term entry (shows space and .) 

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated 

} UIKeyboardType; 

Il codice dovrebbe leggere

if(user is prompted for numeric input only) 
    [textField setKeyboardType:UIKeyboardTypeNumberPad]; 

if(user is prompted for alphanumeric input) 
    [textField setKeyboardType:UIKeyboardTypeDefault]; 
+0

Wow. Incredibilmente semplice. Grazie! –

+6

Si noti che ciò non impedisce a un utente intelligente/disturbato di immettere altri caratteri. Ad esempio: se la tastiera Emoji era attiva * prima * si toccava il campo del numero, sono liberi di digitare facce sorridenti. Non c'è niente che puoi fare su questo, ed è sicuramente il bug di Apple, ma tu ** dovresti ** assicurarti che il tuo codice non si arresti se ottieni numeri diversi in un campo numerico. –

+0

Scoperta oggi, questa è una proprietà del protocollo 'UITextInputTraits', che' UITextField' adotta. – rounak

23

Sì è possibile, ad esempio:

[textField setKeyboardType:UIKeyboardTypeNumberPad]; 
2

C'è una proprietà per questo chiamato keyboardType. Quello che vorrete fare è sostituire dove avete le stringhe @"Number Pad e @"Default con UIKeyboardTypeNumberPad e UIKeyboardTypeDefault.

Il nuovo codice dovrebbe essere simile a questa:

if(user is prompted for numeric input only) 
    [textField setKeyboardType:UIKeyboardTypeNumberPad]; 

else if(user is prompted for alphanumeric input) 
    [textField setKeyboardType:UIKeyboardTypeDefault]; 

Good Luck!

5

per rendere il campo di testo accetta alfa numerico solo impostare questa proprietà

textField.keyboardType = UIKeyboardTypeNamePhonePad; 
59

Vale la pena notare che se si desidera un momento incentrato campo per aggiornare digitare immediatamente la tastiera, c'è un passo in più:

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress 

[textField setKeyboardType:UIKeyboardTypeEmailAddress]; 
[textField reloadInputViews]; 

senza la chiamata al reloadInputViews, la tastiera non cambierà fino a quando il campo selezionato (il first responder) perde e riacquista messa a fuoco.

Un elenco completo dei UIKeyboardType valori can be found here, o:

typedef enum : NSInteger { 
    UIKeyboardTypeDefault, 
    UIKeyboardTypeASCIICapable, 
    UIKeyboardTypeNumbersAndPunctuation, 
    UIKeyboardTypeURL, 
    UIKeyboardTypeNumberPad, 
    UIKeyboardTypePhonePad, 
    UIKeyboardTypeNamePhonePad, 
    UIKeyboardTypeEmailAddress, 
    UIKeyboardTypeDecimalPad, 
    UIKeyboardTypeTwitter, 
    UIKeyboardTypeWebSearch, 
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable 
} UIKeyboardType; 
+0

Questa è un'informazione utile da sapere- Suggerirei di fare una domanda di auto-risposta in stile Q & A solo per estrarre le informazioni sul cambio di immissione del campo attualmente focalizzato (è quello che stavo cercando quando ho trovato questa risposta) – Stonz2

+1

È anche vale la pena ricordare che chiamare reloadInputViews sul campo di testo che non è attualmente focalizzato non cambierà immediatamente il tipo di tastiera. Quindi è meglio chiamare [textfield becomeFirstResponder] prima [textField reloadInputViews] – Qiulang

+0

Era '[textField reloadInputViews];' che mi mancava. Grazie! –

1

per le persone che vogliono utilizzare UIDatePicker come input:

UIDatePicker *timePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 0, 0)]; 
[timePicker addTarget:self action:@selector(pickerChanged:) 
    forControlEvents:UIControlEventValueChanged]; 
[_textField setInputView:timePicker]; 

// pickerChanged: 
- (void)pickerChanged:(id)sender { 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"d/M/Y"]; 
    _textField.text = [formatter stringFromDate:[sender date]]; 
} 
6
_textField .keyboardType = UIKeyboardTypeAlphabet; 
_textField .keyboardType = UIKeyboardTypeASCIICapable; 
_textField .keyboardType = UIKeyboardTypeDecimalPad; 
_textField .keyboardType = UIKeyboardTypeDefault; 
_textField .keyboardType = UIKeyboardTypeEmailAddress; 
_textField .keyboardType = UIKeyboardTypeNamePhonePad; 
_textField .keyboardType = UIKeyboardTypeNumberPad; 
_textField .keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
_textField .keyboardType = UIKeyboardTypePhonePad; 
_textField .keyboardType = UIKeyboardTypeTwitter; 
_textField .keyboardType = UIKeyboardTypeURL; 
_textField .keyboardType = UIKeyboardTypeWebSearch; 
6
textFieldView.keyboardType = UIKeyboardType.PhonePad 

Si tratta di una rapida. Anche in modo che questo funzioni correttamente deve essere impostato dopo la textFieldView.delegate = self

0

Questa è la UIKeyboardTypes per Swift 3:

public enum UIKeyboardType : Int { 

    case `default` // Default type for the current input method. 
    case asciiCapable // Displays a keyboard which can enter ASCII characters 
    case numbersAndPunctuation // Numbers and assorted punctuation. 
    case URL // A type optimized for URL entry (shows ./.com prominently). 
    case numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry. 
    case phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers). 
    case namePhonePad // A type optimized for entering a person's name or phone number. 
    case emailAddress // A type optimized for multiple email address entry (shows space @ . prominently). 

    @available(iOS 4.1, *) 
    case decimalPad // A number pad with a decimal point. 

    @available(iOS 5.0, *) 
    case twitter // A type optimized for twitter text entry (easy access to @ #) 

    @available(iOS 7.0, *) 
    case webSearch // A default keyboard type with URL-oriented addition (shows space . prominently). 

    @available(iOS 10.0, *) 
    case asciiCapableNumberPad // A number pad (0-9) that will always be ASCII digits. 


    public static var alphabet: UIKeyboardType { get } // Deprecated 
} 

Questo è un esempio di utilizzare un tipo di tastiera dall'elenco:

textField.keyboardType = .numberPad 
0

modificare a livello di programmazione UITextField tipo tastiera rapida 3,0

lazy var textFieldTF: UITextField = { 

    let textField = UITextField() 
    textField.placeholder = "Name" 
    textField.frame = CGRect(x:38, y: 100, width: 244, height: 30) 
    textField.textAlignment = .center 
    textField.borderStyle = UITextBorderStyle.roundedRect 
    textField.keyboardType = UIKeyboardType.default //keyboard type 
    textField.delegate = self 
    return textField 
}() 
override func viewDidLoad() { 
    super.viewDidLoad() 
    view.addSubview(textFieldTF) 
} 
Problemi correlati