import UIKit
//添加文本输入框的代理协议
class ViewController: UIViewController,UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRect(x: 60, y: 60, width: 200, height: 30)
// 初始化一个文本输入框对像
let textField = UITextField(frame: rect)
// 设置边框样式为圆角矩形
textField.borderStyle = UITextBorderStyle.roundedRect
// 设置占位符(如果没有输入文本,则会显示该文本信息)
textField.placeholder = "Your Email"
// 关闭文本框对象的语法错误提示功能
textField.autocorrectionType = UITextAutocorrectionType.no
// 设置在输入文本时,键盘上回车按钮的类型
textField.returnKeyType = UIReturnKeyType.done
// 设置文本框右侧的清楚按钮,仅在文本编辑时显示
textField.clearButtonMode = UITextFieldViewMode.whileEditing
// 设置文本对象的键盘类型,为系统提供的邮箱类型
textField.keyboardType = UIKeyboardType.emailAddress
// 设置文本框对象的键盘为暗色主题
textField.keyboardAppearance = UIKeyboardAppearance.dark
// 设置文本框代理为当前视图控制器
textField.delegate = self
self.view.addSubview(textField)
}
// 添加一个方法,当按下回车按钮时调用刺方法
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// 当按下回车键时文本对像失去焦点,键盘自动隐藏
textField.resignFirstResponder()
return true
}