swift中UITextField的使用

swift中UITextField的使用,第1张

概述let textfield = UITextField(frame: CGRectMake(10.0, 10.0, 200.0,40.0))self.view.addSubview(textfield) // 字体属性设置textfield.textColor = UIColor.blackColor()textfield.font = UIFont(name: "GillSans", si
let textfIEld = UITextFIEld(frame: CGRectMake(10.0,10.0,200.0,40.0))self.vIEw.addSubvIEw(textfIEld)
// 字体属性设置textfIEld.textcolor = UIcolor.blackcolor()textfIEld.Font = UIFont(name: "GillSans",size: 15.0)textfIEld.textAlignment = NSTextAlignment.lefttextfIEld.placeholder = "textfIEld的使用"textfIEld.secureTextEntry = false
<pre name="code" >// 光标颜色textfIEld.textcolor = UIcolor.greencolor()
 
textfIEld.enabled = truetextfIEld.userIn@R_502_6704@ctionEnabled = true
// 样式背景属性textfIEld.backgroundcolor = UIcolor.lightGraycolor()textfIEld.borderStyle = UITextborderStyle.line// let image = UIImage(named: "normalimage")// textfIEld.background = image
// 图标let leftvIEw = UIImageVIEw(image: UIImage(named: "normalimage"))leftvIEw.frame = CGRectMake(0.0,0.0,30.0,30.0)textfIEld.leftviewmode = UITextFIEldviewmode.AlwaystextfIEld.leftVIEw = leftvIEwlet rightvIEw = UIImageVIEw(image: UIImage(named: "hightimage"))rightvIEw.frame = CGRectMake(0.0,30.0)textfIEld.rightviewmode = UITextFIEldviewmode.AlwaystextfIEld.rightVIEw = rightvIEw
// 代理,注意添加代理协议,及实现代理方法textfIEld.delegate = self
// 添加协议class VIEwController: UIVIEwController,UITextFIEldDelegate {    overrIDe func vIEwDIDLoad() {    }}
// 代理方法// MARK: - UITextFIEldDelegatefunc textFIEldShouldBeginEditing(textFIEld: UITextFIEld) -> Bool {                print("1 textFIEldShouldBeginEditing")                return true}    func textFIEldDIDBeginEditing(textFIEld: UITextFIEld) {        print("2 textFIEldDIDBeginEditing")}    func textFIEld(textFIEld: UITextFIEld,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool {        print("3 textFIEld")                return true}func textFIEldShouldEndEditing(textFIEld: UITextFIEld) -> Bool {        print("4 textFIEldShouldEndEditing")        print("text:\(textFIEld.text) length = \(textFIEld.text?.characters.count)")        return true}    func textFIEldDIDEndEditing(textFIEld: UITextFIEld) {        print("5 textFIEldDIDEndEditing")}    func textFIEldShouldClear(textFIEld: UITextFIEld) -> Bool {                print("6 textFIEldShouldClear")                return true}    func textFIEldShouldReturn(textFIEld: UITextFIEld) -> Bool {                // 结束编辑        textFIEld.resignFirstResponder()                print("7 textFIEldShouldReturn")                return true}
// 编辑属性设置textfIEld.clearbuttonMode = UITextFIEldviewmode.WhileEditing
// 输入设置textfIEld.keyboardType = UIKeyboardType.WebSearchtextfIEld.returnKeyType = UIReturnKeyType.Done        // 自定义输入源控件// let inputvIEw = UIbutton(frame: CGRectMake(0.0,CGRectGetWIDth(self.vIEw.bounds),100.0))// inputvIEw.setimage(UIImage(named: "normalimage"),forState: UIControlState.normal)// inputvIEw.backgroundcolor = UIcolor.lightGraycolor()// inputvIEw.addTarget(self,action: Selector("click:"),forControlEvents: UIControlEvents.touchUpInsIDe)// textfIEld.inputVIEw = inputvIEw// 自定义输入源控件副视图let accessoryvIEw = UIVIEw(frame: CGRectMake(0.0,40.0))accessoryvIEw.backgroundcolor = UIcolor.greencolor()let accessoryleft = UIbutton(frame: CGRectMake(10.0,60.0,20.0))accessoryvIEw.addSubvIEw(accessoryleft)accessoryleft.setTitle("取消",forState: UIControlState.normal)accessoryleft.backgroundcolor = UIcolor.orangecolor()accessoryleft.addTarget(self,action: Selector("leftClick:"),forControlEvents: UIControlEvents.touchUpInsIDe)let accessoryRight = UIbutton(frame: CGRectMake((CGRectGetWIDth(accessoryvIEw.bounds) - 10.0 - 60.0),20.0))accessoryvIEw.addSubvIEw(accessoryRight)accessoryRight.setTitle("确定",forState: UIControlState.normal)accessoryRight.backgroundcolor = UIcolor.orangecolor()accessoryRight.addTarget(self,action: Selector("rightClick:"),forControlEvents: UIControlEvents.touchUpInsIDe)textfIEld.inputAccessoryVIEw = accessoryvIEw
// 自定义输入源控件时响应事件// MARK: - clickfunc click(button:UIbutton){        self.vIEw.endEditing(true)}    //MARK: - left/right clickfunc leftClick(button:UIbutton){        print("取消")}    func rightClick(button:UIbutton){        self.vIEw.endEditing(true)        print("确定")}
// 其他// 第一响应,即进入编辑状态// textfIEld.becomeFirstResponder()// 结束响应,即结束编辑// textfIEld.resignFirstResponder()// 发通知-输入改变NSNotificationCenter.defaultCenter().addobserver(self,selector: Selector("textfiledEditChanged:"),name: UITextFIEldTextDIDChangeNotification,object: textfIEld)
// 通知响应方法-限制输入长度// MARK: - 通知响应方法func textfiledEditChanged(notification:NSNotification){        let textFIEld:UITextFIEld! = notification.object as! UITextFIEld        if (textFIEld != nil)        {            let text:String! = textFIEld.text            let length = text.characters.count            if (length > 20)            {                textFIEld.text = text.substringToIndex(text.startIndex.advancedBy(20))            }        }}


注意:不能同时设置clearbuttonModerightviewmode/rightVIEw属性,否则只有rightviewmode/rightVIEw有效。


总结

以上是内存溢出为你收集整理的swift中UITextField的使用全部内容,希望文章能够帮你解决swift中UITextField的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071773.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存