您的位置:首页 > 产品设计 > UI/UE

Xcode9学习笔记27 - 文本输入框控件的使用UITextField

2017-11-04 07:42 507 查看


//添加文本框代理协议UITextFieldDelegate,使用协议中的方法,在完成文本框文字的输入后,隐藏系统键盘的显示
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: 80, 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
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: