以编程方式更改UITextField键盘类型

eri*_*ell 174 iphone uitextfield uikeyboard ios

是否可以以编程方式更改uitextfield的键盘类型,以便这样的事情可能:

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

if(user is prompted for alphanumeric input)
    [textField setKeyboardType: @"Default"];
Run Code Online (Sandbox Code Playgroud)

Pen*_*One 368

有一个keyboardType属性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;
Run Code Online (Sandbox Code Playgroud)

你的代码应该阅读

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

if(user is prompted for alphanumeric input)
    [textField setKeyboardType:UIKeyboardTypeDefault];
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这不会阻止聪明/疯狂的用户输入其他字符.例如:如果表情符号键盘处于活动状态*之前*他们点击了您的数字字段,他们可以自由地在其中键入笑脸.关于这一点你无能为力,这肯定是Apple的错误,但你应该**确保你的代码在数字字段中得到非数字时不会崩溃. (7认同)

jte*_*rry 74

值得注意的是,如果你想要一个当前关注的字段立即更新键盘类型,还有一个额外的步骤:

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress

[textField setKeyboardType:UIKeyboardTypeEmailAddress];
[textField reloadInputViews];
Run Code Online (Sandbox Code Playgroud)

没有调用reloadInputViews,键盘将不会改变,直到所选字段(第一响应者)失去并重新获得焦点.

可以在此处找到完整的UIKeyboardType值列表,或者:

typedef enum : NSInteger {
    UIKeyboardTypeDefault,
    UIKeyboardTypeASCIICapable,
    UIKeyboardTypeNumbersAndPunctuation,
    UIKeyboardTypeURL,
    UIKeyboardTypeNumberPad,
    UIKeyboardTypePhonePad,
    UIKeyboardTypeNamePhonePad,
    UIKeyboardTypeEmailAddress,
    UIKeyboardTypeDecimalPad,
    UIKeyboardTypeTwitter,
    UIKeyboardTypeWebSearch,
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;
Run Code Online (Sandbox Code Playgroud)


Ala*_*ore 23

是的,你可以,例如:

[textField setKeyboardType:UIKeyboardTypeNumberPad];
Run Code Online (Sandbox Code Playgroud)


小智 9

    textFieldView.keyboardType = UIKeyboardType.PhonePad
Run Code Online (Sandbox Code Playgroud)

这是为了迅速.另外为了使其正常工作,必须在之后设置textFieldView.delegate = self


Hos*_*eeb 7

要使文本字段接受字母数字,只设置此属性

textField.keyboardType = UIKeyboardTypeNamePhonePad;
Run Code Online (Sandbox Code Playgroud)


小智 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;
Run Code Online (Sandbox Code Playgroud)


San*_*Ray 5

斯威夫特4

如果要在满足条件时尝试更改键盘类型,请遵循此步骤。例如:如果要在文本字段的计数为4或5时将键盘类型从“ 默认”更改为“ 数字键盘”,请执行以下操作:

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()
 }
Run Code Online (Sandbox Code Playgroud)