classViewController:UIViewController,UITextFieldDelegate{
overridefuncviewDidLoad() {
super.viewDidLoad()
lettextField =UITextField.init(frame:CGRect(x:10, y:60, width:200, height:20))
textField.borderStyle=UITextBorderStyle.roundedRect
textField.placeholder="请输入用户名"
textField.adjustsFontSizeToFitWidth=true//当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize=14//最小可缩小的字号
textField.textAlignment= .right//水平右对齐
textField.delegate=self
/**水平对齐
textField.textAlignment = .center //水平居中对齐
textField.textAlignment = .left //水平左对齐
**/
/**垂直对齐
textField.contentVerticalAlignment = .top//垂直向上对齐
textField.contentVerticalAlignment = .center//垂直居中对齐
textField.contentVerticalAlignment = .bottom//垂直向下对齐
**/
textField.clearButtonMode= .whileEditing//编辑时出现清除按钮
/*
textField.clearButtonMode = .unlessEditing//编辑时不出现,编辑后才出现清除按钮
textField.clearButtonMode = .always//一直显示清除按钮
*/
textField.returnKeyType=UIReturnKeyType.go
/*
textField.returnKeyType = UIReturnKeyType.done //表示完成输入
textField.returnKeyType = UIReturnKeyType.go //表示完成输入,同时会跳到另一页
textField.returnKeyType = UIReturnKeyType.search //表示搜索
textField.returnKeyType = UIReturnKeyType.join //表示注册用户或添加数据
textField.returnKeyType = UIReturnKeyType.next //表示继续下一步
textField.returnKeyType = UIReturnKeyType.send //表示发送
*/
self.view.addSubview(textField)
}
functextFieldShouldReturn(_textField:UITextField) ->Bool{
//收起键盘
textField.resignFirstResponder()
//打印出文本框中的值
print(textField.text??"没有")
returntrue;
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
网友评论