2011-09-04 14 views
157

Czy można programowo zmienić typ klawiaturze UITextField tak, że coś takiego byłoby możliwe:Programowo zmienić UITextField typ klawiatury

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

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

Proponuję zmienić termin 'doozy' na coś, co jest bardziej zrozumiałe ..pamiętaj, że SO to strona międzynarodowa, a nie północnoamerykańska – abbood

Odpowiedz

344

Jest keyboardType nieruchomość na 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; 

Kod powinien przeczytać

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

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

Wow. Niezwykle proste. Dziękuję Ci! –

+6

Pamiętaj, że to nie powstrzyma sprytnego/obłąkanego użytkownika przed wprowadzaniem innych znaków. Na przykład: jeśli klawiatura Emoji była aktywna * przed * stukały w twoje pole liczbowe, mogą wpisywać buźki. Nic nie możesz na to poradzić, i na pewno jest to błąd Apple'a, ale ** powinieneś ** upewnić się, że twój kod się nie zawiesza, jeśli w polu liczbowym nie ma liczb. –

+0

Dzisiaj jest to właściwość protokołu 'UITextInputTraits', który przyjmuje' UITextField'. – rounak

23

Tak można na przykład:

[textField setKeyboardType:UIKeyboardTypeNumberPad]; 
2

Jest to właściwość o nazwie keyboardType. Co chcesz zrobić, to zastąpić, gdzie masz ciągi znaków @"Number Pad i @"Default z UIKeyboardTypeNumberPad i UIKeyboardTypeDefault.

Twój nowy kod powinien wyglądać mniej więcej tak:

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

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

powodzenia!

5

aby pole tekstowe zaakceptować alfa numeryczny tylko ustawić tę właściwość

textField.keyboardType = UIKeyboardTypeNamePhonePad; 
59

Warto zauważyć, że jeśli chcesz obecnie koncentruje pole zaktualizować klawiatura typu natychmiast, jeszcze jeden dodatkowy krok:

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress 

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

Bez wywołaniu reloadInputViews, klawiatura nie zmieni się aż do wybranego pola (the first responder) traci i odzyskuje ostrość.

Pełną listę wartości UIKeyboardTypecan be found here lub:

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

Jest to przydatna informacja, którą warto znać - Proponuję odpowiedzieć sobie na pytanie z pytaniami i odpowiedziami w stylu Q & A tylko po to, aby wyodrębnić informacje o aktualnie skupionej zmianie pola wejściowego (tego właśnie szukałem, gdy znalazłem tę odpowiedź). – Stonz2

+1

Jest również Warto wspomnieć, że wywoływanie reloadInputViews na polu tekstowym, które NIE jest aktualnie skupione, nie zmieni natychmiast typu klawiatury. Lepiej zadzwoń najpierw [textfield sięFirstResponder] następnie [textField reloadInputViews] – Qiulang

+0

To było "[textField reloadInputViews];", którego mi brakowało. Dzięki! –

1

dla ludzi, którzy chcą korzystać UIDatePicker jako wejście:

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 

To jest dla szybkich. Również, aby ta działała poprawnie należy ustawić po textFieldView.delegate = self

0

To UIKeyboardTypes dla 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 
} 

To jest przykład użyć typ klawiatury z listy:

textField.keyboardType = .numberPad 
0

Programowo zmienić UITextField typ klawiatury szybkiej 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) 
}