- 前提:必须使用系统自带键盘
- 前提:必须使用系统自带键盘
- 前提:必须使用系统自带键盘
番外:强制使用系统键盘类型方法
在 AppDelegate 里面 UIApplicationDelegate 代理方法中设置
func application(_ application: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplication.ExtensionPointIdentifier) -> Bool {
if extensionPointIdentifier.rawValue == "com.apple.keyboard-service" {
return false
}
return true
}
iOS 10.0+ 新特性,系统键盘读取当前手机 SIM 卡号
图例:
- 点击键盘上方的手机号码,即可自动填充到输入框 (如果输入框对数据进行的限制,需要额外处理)
实现方法讲解:
设置键盘类型为 UIKeyboardType.numbersAndPunctuation
设置输入框的内容类型为 UITextContentType.telephoneNumber
具体代码:
if #available(iOS 10.0, *) {
phoneNumberField.keyboardType = .numbersAndPunctuation
phoneNumberField.textContentType = .telephoneNumber
} else {
phoneNumberField.keyboardType = .numberPad
}
键盘弹出的类型
图例:
- 点击键盘上方的验证码,即可自动填充到输入框
实现方法讲解:
设置输入框的内容类型为 UITextContentType.oneTimeCode
具体代码:
if #available(iOS 12.0, *) {
codeField.textContentType = .oneTimeCode
}
键盘弹出的类型
详情内容请查看之前的介绍
输入框内容类型列举如下
extension UITextContentType {
/*
......
*/
/// 电话号码
@available(iOS 10.0, *)
public static let telephoneNumber: UITextContentType
/// 邮箱地址
@available(iOS 10.0, *)
public static let emailAddress: UITextContentType
/// 网址
@available(iOS 10.0, *)
public static let URL: UITextContentType
/// 信用卡号
@available(iOS 10.0, *)
public static let creditCardNumber: UITextContentType
/// 用户名
@available(iOS 11.0, *)
public static let username: UITextContentType
/// 密码
@available(iOS 11.0, *)
public static let password: UITextContentType
/// 新密码
@available(iOS 12.0, *)
public static let newPassword: UITextContentType
/// 验证码
@available(iOS 12.0, *)
public static let oneTimeCode: UITextContentType
}