编程改变的UITextField键盘类型
是否有可能以编程方式更改的UITextField的键盘类型,所以这样的事情是可能的:编程改变的UITextField键盘类型
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];
哇。非常简单。谢谢! –
请注意,这不会阻止聪明/错误的用户输入其他字符。例如:如果表情符号键盘在*之前处于活动状态*他们点击您的号码字段,他们可以*地在其中输入笑脸。对此,你无能为力,这绝对是苹果的错误,但你应该**确保你的代码不会崩溃,如果你在数字字段中得到非数字。 –
今天发现,这是'UITextField'采用的'UITextInputTraits'协议的属性。 – rounak
是你可以,例如:
[textField setKeyboardType:UIKeyboardTypeNumberPad];
有一个属性为这keyboardType
。 你想要做的是取代你的字符串@"Number Pad
和@"Default
与UIKeyboardTypeNumberPad
和UIKeyboardTypeDefault
。
你的新代码应该是这个样子:
if(user is prompted for numeric input only)
[textField setKeyboardType:UIKeyboardTypeNumberPad];
else if(user is prompted for alphanumeric input)
[textField setKeyboardType:UIKeyboardTypeDefault];
祝您好运!
,以使文本字段接受字母数字只设置该属性
textField.keyboardType = UIKeyboardTypeNamePhonePad;
值得一提的是,如果你想有一个当前重点场更新键盘立即输入,还有一个额外的步骤:
// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress
[textField setKeyboardType:UIKeyboardTypeEmailAddress];
[textField reloadInputViews];
没有调用reloadInputViews
,键盘不会改变,直到选定字段(first responder)失去和获得焦点。
的UIKeyboardType
值can be found here,或的完整列表:
typedef enum : NSInteger {
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
UIKeyboardTypeDecimalPad,
UIKeyboardTypeTwitter,
UIKeyboardTypeWebSearch,
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;
谁想要使用UIDatePicker
作为输人:
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]];
}
_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;
textFieldView.keyboardType = UIKeyboardType.PhonePad
这是迅速的。此外,在为了这个正常工作,必须在
后设置这是UIKeyboardTypes
为雨燕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
}
这是从列表中使用键盘类型的例子:
textField.keyboardType = .numberPad
编程改变的UITextField键盘类型迅速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)
}
我建议你将'doozy'这个词改为更常用的东西。请记住,SO是国际站点,不是北美站点 – abbood