一、设置键盘的种类
textField.keyboardType = UIKeyboardTypeDefault ;
UIKeyboardType:
typedef NS_ENUM(NSInteger, UIKeyboardType) {
//常用于文本输入
UIKeyboardTypeDefault, // Default type for the current input method.
//常用于密码输入,字母表
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters
//数字和各种标点符号(第一排数字,下面标点符号)
UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
//适用于网址输入
UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
//只有数字的数字键盘(iOS10之后新增了波斯和印地语等数字键盘类型的支持)
UIKeyboardTypeNumberPad, // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). 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 API_AVAILABLE(ios(4.1)), // A number pad with a decimal point.
UIKeyboardTypeTwitter API_AVAILABLE(ios(5.0)), // A type optimized for twitter text entry (easy access to @ #)
UIKeyboardTypeWebSearch API_AVAILABLE(ios(7.0)), // A default keyboard type with URL-oriented addition (shows space . prominently).
//iOS10以上,数字键盘
UIKeyboardTypeASCIICapableNumberPad API_AVAILABLE(ios(10.0)), // A number pad (0-9) that will always be ASCII digits.
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
};
iOS10之前:
- UIKeyboardTypeNumberPad表示纯数字键盘。
iOS10之后:
- 新增UIKeyboardTypeASCIICapableNumberPad这种数字键盘类型,类似于之前的UIKeyboardTypeNumberPad。
- 对UIKeyboardTypeNumberPad新增了波斯和印地语等数字键盘类型的支持。左下角有个🌍图标切换。
另:如何让输入的所有字母均显示大写?
UITextField有一个属性:autocapitalizationType (首字母是否大写)
设置为UITextAutocapitalizationTypeAllCharacters
即可
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
};
二、自定义“按键命令”
textField.returnKeyType = UIReturnKeyNext;
UIReturnKeyType:
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
//后面的注释是键盘对应的中文显示
UIReturnKeyDefault, //默认显示换行
UIReturnKeyGo, //前往
UIReturnKeyGoogle, //搜索
UIReturnKeyJoin, //加入
UIReturnKeyNext, //下一项
UIReturnKeyRoute, //路线
UIReturnKeySearch, //搜索
UIReturnKeySend, //发送
UIReturnKeyYahoo, //搜索
UIReturnKeyDone, //完成
UIReturnKeyEmergencyCall, //紧急电话
UIReturnKeyContinue API_AVAILABLE(ios(9.0)), //继续
};
三、UITextField (Edits text) 唤起键盘
self.textField.keyboardType = UIKeyboardTypeNumberPad;
[self.textField becomeFirstResponder];
// 注意要考虑 keyWindow 的问题,如果当前window不是keyWindow,调用了上面方法也不会弹出键盘。
四、收回键盘
- 方法一:撤销textfield的第一响应者
谁叫出的键盘,谁就是第一响应者,有多少个就要写几次
[self.textField1 resignFirstReponder];
[self.textField2 resignFirstReponder];
- 方法二:让整个view停止编辑
这时view的所有子控件叫出的键盘,就会收回
[self.view endEditing:YES];
- 方法三:直接停止keyWindow的编辑
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];