177

Is it possible to programmatically change the keyboard type of a uitextfield so that something like this would be possible:

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

if(user is prompted for alphanumeric input)
    [textField setKeyboardType: @"Default"];
eric.mitchell
  • 8,501
  • 12
  • 50
  • 91
  • 3
    i'd suggest you change the term `doozy` to something that's more commonly understandable.. keep in mind SO is an international site and not a North American one – abbood Jan 21 '14 at 09:30
  • Possible duplicate of [Programmatically changing keyboard type on iOS does nothing](https://stackoverflow.com/questions/12619375/programmatically-changing-keyboard-type-on-ios-does-nothing) – Parth Patel Jul 11 '19 at 09:48

12 Answers12

374

There is a keyboardType property for a 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;

Your code should read

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

if(user is prompted for alphanumeric input)
    [textField setKeyboardType:UIKeyboardTypeDefault];
kwl
  • 403
  • 5
  • 8
PengOne
  • 47,332
  • 17
  • 126
  • 147
  • 7
    Note that this doesn't stop a clever/deranged user from entering other characters. For example: If the Emoji keyboard was active *before* they tapped your number field, they're free to type smiley faces into it. There's nothing you can do about this, and it's definitely Apple's bug, but you **should** make sure your code doesn't crash if you get non-numbers in a number field. – Steven Fisher Jul 28 '12 at 23:59
  • Found out today, this is a property of the `UITextInputTraits` protocol, which `UITextField` adopts. – rounak Dec 23 '14 at 14:58
  • 1
    Is it possible to programmatically change keyboard type as UIKeyboardTypeNumbersAndPunctuation, for HTML input fields loaded in webview? – Srini Jun 13 '15 at 02:46
  • I created uitextfield programmatically in one project with respective keyboard type. this is woked few days ago. but now this is not worked. Not getting actual reason – Rahul Phate Jun 19 '19 at 11:43
83

It's worth noting that if you want a currently-focused field to update the keyboard type immediately, there's one extra step:

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress

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

Without the call to reloadInputViews, the keyboard will not change until the selected field (the first responder) loses and regains focus.

A full list of the UIKeyboardType values can be found here, or:

typedef enum : NSInteger {
    UIKeyboardTypeDefault,
    UIKeyboardTypeASCIICapable,
    UIKeyboardTypeNumbersAndPunctuation,
    UIKeyboardTypeURL,
    UIKeyboardTypeNumberPad,
    UIKeyboardTypePhonePad,
    UIKeyboardTypeNamePhonePad,
    UIKeyboardTypeEmailAddress,
    UIKeyboardTypeDecimalPad,
    UIKeyboardTypeTwitter,
    UIKeyboardTypeWebSearch,
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;
jterry
  • 5,899
  • 2
  • 27
  • 36
  • This is useful information to know- I would suggest doing a Q&A-style self-answered question just to extract the information about the currently-focused field input change (that's what I was looking for when I found this answer) – Stonz2 Aug 25 '14 at 21:03
  • 1
    It is also worth mentioning that calling reloadInputViews on the textfield that is currently NOT focused won't change keyboard type immediately. So better call [textfield becomeFirstResponder] first then [textField reloadInputViews] – Qiulang Dec 31 '14 at 04:13
  • 1
    It was `[textField reloadInputViews];` that I was missing. Thanks! – Islam Q. Oct 19 '15 at 18:50
  • 1
    Calling `reloadInputViews` also works for bespoke `UITextInput` implementations. – Paul Gardiner Jan 15 '19 at 14:10
23

Yes you can, for example:

[textField setKeyboardType:UIKeyboardTypeNumberPad];
Alan Moore
  • 6,347
  • 6
  • 50
  • 66
10
    textFieldView.keyboardType = UIKeyboardType.PhonePad

This is for swift. Also in order for this to function properly it must be set after the textFieldView.delegate = self

fonz
  • 320
  • 3
  • 10
9

to make the text field accept alpha numeric only set this property

textField.keyboardType = UIKeyboardTypeNamePhonePad;
Hossam Ghareeb
  • 6,622
  • 3
  • 47
  • 61
8

Swift 4

If you are trying to change your keyboard type when a condition is met, follow this. For example: If we want to change the keyboard type from Default to Number Pad when the count of the textfield is 4 or 5, then do this:

textField.addTarget(self, action: #selector(handleTextChange), for: .editingChanged)

@objc func handleTextChange(_ textChange: UITextField) {
 if textField.text?.count == 4 || textField.text?.count == 5 {
   textField.keyboardType = .numberPad
   textField.reloadInputViews() // need to reload the input view for this to work
 } else {
   textField.keyboardType = .default
   textField.reloadInputViews()
 }
Sanket Ray
  • 862
  • 1
  • 9
  • 21
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;
2

There is a property for this called keyboardType. What you'll want to do is replace where you have strings @"Number Pad and @"Default with UIKeyboardTypeNumberPad and UIKeyboardTypeDefault.

Your new code should look something like this:

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

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

Good Luck!

Kyle Rosenbluth
  • 1,582
  • 4
  • 21
  • 38
1

for people who want to use UIDatePicker as 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]];
}
Brian
  • 26,298
  • 13
  • 78
  • 84
1

This is the UIKeyboardTypes for 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
}

This is an example to use a keyboard type from the list:

textField.keyboardType = .numberPad
pableiros
  • 10,752
  • 11
  • 79
  • 78
0

Programmatically change UITextField Keyboard type swift 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)
}
Tanjima Kothiya
  • 4,916
  • 2
  • 6
  • 14
0

Here are the type of keyboard in Swift 4.2

// UIKeyboardType
//
// Requests that a particular keyboard type be displayed when a text widget
// becomes first responder. 
// Note: Some keyboard/input methods types may not support every variant. 
// In such cases, the input method will make a best effort to find a close 
// match to the requested type (e.g. displaying UIKeyboardTypeNumbersAndPunctuation 
// type if UIKeyboardTypeNumberPad is not supported).
//
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
}
Rikesh Subedi
  • 1,441
  • 17
  • 20