UITextField是用来文本输入的,比如常用的登录用户名和密码输入等等,那我们直接进入主题吧
1.创建普通的UITextField
//构建基本的UITextField varuitf:UITextField=UITextField(frame:CGRect(x:50,y:50,width:200,height:30)) uitf.backgroundColor=UIColor.whiteColor() uitf.placeholder="普通UITextField"
2.构建密码输入框
varuitf1:UITextField=UITextField(frame:CGRect(x:50,y:90,height:35)) uitf1.placeholder="密码输入框" uitf1.borderStyle=UITextBorderStyle.RoundedRect//边框样式 uitf1.backgroundColor=UIColor.whiteColor() uitf1.secureTextEntry=true//密码输入框
3.设置UITextField的左边视图
varuitf2:UITextField=UITextField(frame:CGRect(x:50,y:135,height:30)) uitf2.placeholder="请输入购买的物品" uitf2.backgroundColor=UIColor.whiteColor() varimg:UIImageView=UIImageView(image:UIImage(named:"buy.png")!) img.frame=CGRect(x:5,y:5,width:40,height:30) img.contentMode=UIViewContentMode.ScaleAspectFit uitf2.leftView=img uitf2.leftviewmode=UITextFieldviewmode.Always uitf2.leftViewRectForBounds(CGRect(x:0,y:0,width:44,height:44))
4.设置UITextField的右边边视图
varuitf3:UITextField=UITextField(frame:CGRect(x:50,y:175,height:35)) uitf3.placeholder="请输入购买的物品" uitf3.backgroundColor=UIColor.whiteColor() uitf3.layer.cornerRadius=2 uitf3.layer.borderWidth=1 uitf3.layer.borderColor=UIColor.greenColor().CGColor //uitf3.borderStyle=UITextBorderStyle.RoundedRect varimg1:UIImageView=UIImageView(image:UIImage(named:"buy.png")!) img1.frame=CGRect(x:5,height:30) img1.contentMode=UIViewContentMode.ScaleAspectFit uitf3.rightView=img1 uitf3.rightviewmode=UITextFieldviewmode.Always
5.委托事件,及加入clear按钮
varuitf4:UITextField=UITextField(frame:CGRect(x:50,y:220,height:35)) uitf4.backgroundColor=UIColor.whiteColor() uitf4.delegate=self uitf4.clearButtonMode=UITextFieldviewmode.WhileEditing
当鼠标进入UITextField时,响应textFieldShouldBeginEditing -> textFieldDidBeginEditing
当鼠标退出UITextField时,响应textFieldShouldEndEditing -> textFieldDidEndEditing
当输入文本内容时,响应textField
在输入框里,在虚拟键盘上点击return时,响应textFieldShouldReturn
在输入框里,点击UITextField的clear按钮,响应textFieldShouldClear
functextField(textField:UITextField,shouldChangeCharactersInRangerange:NSRange,replacementStringstring:String)->Bool{ NSLog("textField") returntrue } functextFieldShouldBeginEditing(textField:UITextField)->Bool{ NSLog("textFieldShouldBeginEditing") returntrue } functextFieldDidBeginEditing(textField:UITextField){ NSLog("textFieldDidBeginEditing") } functextFieldShouldEndEditing(textField:UITextField)->Bool{ NSLog("textFieldShouldEndEditing") returntrue } functextFieldDidEndEditing(textField:UITextField){ NSLog("textFieldDidEndEditing") } functextFieldShouldClear(textField:UITextField)->Bool{ NSLog("textFieldShouldClear") returntrue } functextFieldShouldReturn(textField:UITextField)->Bool{ NSLog("textFieldShouldReturn") textField.resignFirstResponder()//这个隐藏(放弃)虚拟键盘 returntrue }
全部代码
importUIKit classViewController:UIViewController,UITextFieldDelegate{ overridefuncdidReceiveMemoryWarning(){ super.didReceiveMemoryWarning() //DispoSEOfanyresourcesthatcanberecreated. } overridefuncviewDidLoad(){ super.viewDidLoad() //Doanyadditionalsetupafterloadingtheview,typicallyfromanib. //构建基本的UITextField varuitf:UITextField=UITextField(frame:CGRect(x:50,height:30)) uitf.backgroundColor=UIColor.whiteColor() uitf.placeholder="普通UITextField" self.view.addSubview(uitf) //构建密码输入框 varuitf1:UITextField=UITextField(frame:CGRect(x:50,height:35)) uitf1.placeholder="密码输入框" uitf1.borderStyle=UITextBorderStyle.RoundedRect//边框样式 uitf1.backgroundColor=UIColor.whiteColor() uitf1.secureTextEntry=true//密码输入框 self.view.addSubview(uitf1) //设置UITextField的左边视图 varuitf2:UITextField=UITextField(frame:CGRect(x:50,height:44)) self.view.addSubview(uitf2) //设置UITextField的右边边视图 varuitf3:UITextField=UITextField(frame:CGRect(x:50,height:30) img1.contentMode=UIViewContentMode.ScaleAspectFit uitf3.rightView=img1 uitf3.rightviewmode=UITextFieldviewmode.Always self.view.addSubview(uitf3) //委托事件 varuitf4:UITextField=UITextField(frame:CGRect(x:50,height:35)) uitf4.backgroundColor=UIColor.whiteColor() uitf4.delegate=self uitf4.clearButtonMode=UITextFieldviewmode.WhileEditing self.view.addSubview(uitf4) } //////// //当鼠标进入UITextField时,响应textFieldShouldBeginEditing->textFieldDidBeginEditing //当鼠标退出UITextField时,响应textFieldShouldEndEditing->textFieldDidEndEditing //当输入文本内容时,响应textField //在输入框里,响应textFieldShouldReturn //在输入框里,响应textFieldShouldClear functextField(textField:UITextField,replacementStringstring:String)->Bool{ NSLog("textField") returntrue } functextFieldShouldBeginEditing(textField:UITextField)->Bool{ NSLog("textFieldShouldBeginEditing") returntrue } functextFieldDidBeginEditing(textField:UITextField){ NSLog("textFieldDidBeginEditing") } functextFieldShouldEndEditing(textField:UITextField)->Bool{ NSLog("textFieldShouldEndEditing") returntrue } functextFieldDidEndEditing(textField:UITextField){ NSLog("textFieldDidEndEditing") } functextFieldShouldClear(textField:UITextField)->Bool{ NSLog("textFieldShouldClear") returntrue } functextFieldShouldReturn(textField:UITextField)->Bool{ NSLog("textFieldShouldReturn") textField.resignFirstResponder()//这个隐藏(放弃)虚拟键盘 returntrue } }
效果图
转载自吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=98