Programmatically change UITextField Keyboard type

前端 未结 12 1263
不知归路
不知归路 2020-11-28 19:28

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 i         


        
相关标签:
12条回答
  • 2020-11-28 19:33

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

    textField.keyboardType = UIKeyboardTypeNamePhonePad;
    
    0 讨论(0)
  • 2020-11-28 19:34

    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()
     }
    
    0 讨论(0)
  • 2020-11-28 19:35

    Yes you can, for example:

    [textField setKeyboardType:UIKeyboardTypeNumberPad];
    
    0 讨论(0)
  • 2020-11-28 19:38

    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
    
    0 讨论(0)
  • 2020-11-28 19:39
        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

    0 讨论(0)
  • 2020-11-28 19:42

    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;
    
    0 讨论(0)
提交回复
热议问题