iOS UITextField
import UIKit class ViewController: UIViewController,UITextFieldDelegate{ var exampleTextField : UITextField! override func viewDidLoad() { super.viewDidLoad() //初始化textField self.exampleTextField = UITextField.init() self.exampleTextField.frame = CGRectMake(20, 100, 300, 30) self.exampleTextField.placeholder = "占位符啊" self.exampleTextField.delegate = self self.exampleTextField.borderStyle = UITextBorderStyle.RoundedRect /* UITextBorderStyle.None:无边框 UITextBorderStyle.Line:直线边框 UITextBorderStyle.RoundedRect:圆角矩形边框 UITextBorderStyle.Bezel:边线+阴影 */ self.exampleTextField.adjustsFontSizeToFitWidth=true //当文字超出文本框宽度时,自动调整文字大小 self.exampleTextField.minimumFontSize=14 //最小可缩小的字号 /** 水平对齐 **/ self.exampleTextField.textAlignment = .Right //水平右对齐 //Center //水平居中对齐 //Left //水平左对齐 /** 垂直对齐 **/ self.exampleTextField.contentVerticalAlignment = .Top //垂直向上对齐 //Center //垂直居中对齐 //Bottom //垂直向下对齐 self.exampleTextField.clearButtonMode=UITextFieldViewMode.WhileEditing //编辑时出现清除按钮 //self.exampleTextField.clearButtonMode=UITextFieldViewMode.UnlessEditing //编辑时不出现,编辑后才出现清除按钮 //self.exampleTextField.clearButtonMode=UITextFieldViewMode.Always //一直显示清除按钮 self.exampleTextField.keyboardType = UIKeyboardType.NumberPad /* 设置文本框关联的键盘类型 Default:系统默认的虚拟键盘 ASCII Capable:显示英文字母的虚拟键盘 Numbers and Punctuation:显示数字和标点的虚拟键盘 URL:显示便于输入数字的虚拟键盘 Number Pad:显示便于输入数字的虚拟键盘 Phone Pad:显示便于拨号呼叫的虚拟键盘 Name Phone Pad:显示便于聊天拨号的虚拟键盘 Email Address:显示便于输入Email的虚拟键盘 Decimal Pad:显示用于输入数字和小数点的虚拟键盘 Twitter:显示方便些Twitter的虚拟键盘 Web Search:显示便于在网页上书写的虚拟键盘 */ self.exampleTextField.returnKeyType = .Search /* textField.returnKeyType = UIReturnKeyType.Done //表示完成输入 textField.returnKeyType = UIReturnKeyType.Go //表示完成输入,同时会跳到另一页 textField.returnKeyType = UIReturnKeyType.Search //表示搜索 textField.returnKeyType = UIReturnKeyType.Join //表示注册用户或添加数据 textField.returnKeyType = UIReturnKeyType.Next //表示继续下一步 textField.returnKeyType = UIReturnKeyType.Send //表示发送 */ //成为响应者,弹出输入键盘
textField.becomeFirstResponder()
//失去第一响应者
textField.resignFirstResponder()
self.view .addSubview(self.exampleTextField) // Do any additional setup after loading the view, typically from a nib. } func textFieldDidBeginEditing(textField: UITextField) { print("\(textField.text)") } func textFieldShouldReturn(textField: UITextField) -> Bool { print("\(textField.text)") var str : String str = textField.text! print("\(str)") self.exampleTextField.resignFirstResponder()//收起键盘 return false } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
原文链接: https://blog.csdn.net/qq_35122556/java/article/details/80020324