UITextFIEld是用来文本输入的,比如常用的登录用户名和密码输入等等,那我们直接进入主题吧
1.创建普通的UITextFIEld
//构建基本的UITextFIEldvaruitf: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.ScaleAspectFituitf2.leftVIEw=imguitf2.leftviewmode=UITextFIEldviewmode.Alwaysuitf2.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=2uitf3.layer.borderWIDth=1uitf3.layer.bordercolor=UIcolor.greencolor().CGcolor//uitf3.borderStyle=UITextborderStyle.RoundedRectvarimg1:UIImageVIEw=UIImageVIEw(image:UIImage(named:"buy.png")!)img1.frame=CGRect(x:5,height:30)img1.contentMode=UIVIEwContentMode.ScaleAspectFituitf3.rightVIEw=img1uitf3.rightviewmode=UITextFIEldviewmode.Always
varuitf4:UITextFIEld=UITextFIEld(frame:CGRect(x:50,y:220,height:35))uitf4.backgroundcolor=UIcolor.whitecolor()uitf4.delegate=selfuitf4.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}
全部代码
importUIKitclassVIEwController:UIVIEwController,UITextFIEldDelegate{overrIDefuncdIDReceiveMemoryWarning(){super.dIDReceiveMemoryWarning()//dispoSEOfanyresourcesthatcanberecreated.}overrIDefuncvIEwDIDLoad(){super.vIEwDIDLoad()//DoanyadditionalsetupafterloadingthevIEw,typicallyfromanib.//构建基本的UITextFIEldvaruitf: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.ScaleAspectFituitf3.rightVIEw=img1uitf3.rightviewmode=UITextFIEldviewmode.Alwaysself.vIEw.addSubvIEw(uitf3)//委托事件varuitf4:UITextFIEld=UITextFIEld(frame:CGRect(x:50,height:35))uitf4.backgroundcolor=UIcolor.whitecolor()uitf4.delegate=selfuitf4.clearbuttonMode=UITextFIEldviewmode.WhileEditingself.vIEw.addSubvIEw(uitf4)}//////////当鼠标进入UITextFIEld时,响应textFIEldShouldBeginEditing->textFIEldDIDBeginEditing//当鼠标退出UITextFIEld时,响应textFIEldShouldEndEditing->textFIEldDIDEndEditing//当输入文本内容时,响应textFIEld//在输入框里,响应textFIEldShouldReturn//在输入框里,响应textFIEldShouldClearfunctextFIEld(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}}
效果图
650) this.wIDth=650;" alt="865E70FE-173F-4942-81B7-1DF68487C16C.png" src="http://img.jb51.cc/vcimg/static/loading.png" title="1427958750028029966.png" src="http://www.wutongwei.com/ueditor/Jsp/upload/image/20150402/1427958750028029966.png">
转载自吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?ID=98
总结以上是内存溢出为你收集整理的The Swift Code之UITextField的使用,及事件委托全部内容,希望文章能够帮你解决The Swift Code之UITextField的使用,及事件委托所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)