以编程方式更改UITextField键盘types
是否有可能以编程方式更改uitextfield的键盘types,以便这样的事情是可能的:
if(user is prompted for numeric input only) [textField setKeyboardType: @"Number Pad"]; if(user is prompted for alphanumeric input) [textField setKeyboardType: @"Default"];
有一个UITextField
的keyboardType
属性:
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;
你的代码应该阅读
if(user is prompted for numeric input only) [textField setKeyboardType:UIKeyboardTypeNumberPad]; if(user is prompted for alphanumeric input) [textField setKeyboardType:UIKeyboardTypeDefault];
值得注意的是,如果您想要一个当前焦点的字段立即更新键盘types,那么还有一个额外的步骤:
// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress [textField setKeyboardType:UIKeyboardTypeEmailAddress]; [textField reloadInputViews];
如果没有调用reloadInputViews
,键盘将不会改变,直到选定的字段(第一个响应者 )失去并重新获得焦点。
可以在这里find完整的UIKeyboardType
值列表,或者:
typedef enum : NSInteger { UIKeyboardTypeDefault, UIKeyboardTypeASCIICapable, UIKeyboardTypeNumbersAndPunctuation, UIKeyboardTypeURL, UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad, UIKeyboardTypeNamePhonePad, UIKeyboardTypeEmailAddress, UIKeyboardTypeDecimalPad, UIKeyboardTypeTwitter, UIKeyboardTypeWebSearch, UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;
是的,你可以,例如:
[textField setKeyboardType:UIKeyboardTypeNumberPad];
textFieldView.keyboardType = UIKeyboardType.PhonePad
这是快速的。 另外为了使其正常工作,必须在textFieldView.delegate = self
之后进行设置
使文本字段接受字母数字只设置此属性
textField.keyboardType = UIKeyboardTypeNamePhonePad;
_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;
有一个属性叫做keyboardType
。 你想要做的是用UIKeyboardTypeNumberPad
和UIKeyboardTypeDefault
replace你有string@"Number Pad
和@"Default
UIKeyboardTypeDefault
。
你的新代码应该是这样的:
if(user is prompted for numeric input only) [textField setKeyboardType:UIKeyboardTypeNumberPad]; else if(user is prompted for alphanumeric input) [textField setKeyboardType:UIKeyboardTypeDefault];
祝你好运!
对于想要使用UIDatePicker
作为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]]; }
这是Swift 3的UIKeyboardTypes
:
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 }
这是使用列表中键盘types的一个示例:
textField.keyboardType = .numberPad