Swift UITextFieldUITextView(placeholder的制作)

Swift UITextFieldUITextView(placeholder的制作),第1张

概述UITextField 一个UITextField对象在你的界面上显示一个可编辑的文本区域。你使用的文本字段收集文本输入使用屏幕键盘的用户。键盘是可配置的许多不同类型的输入,如纯文本,电子邮件,数字等。文本字段使用目标 *** 作机制和一个委托对象来报告编辑过程中所做的更改。 UITextField,系统给我们提供了四中样式: 在使用上,我们可以使用系统提供样式,也可以自己定义。在使用上我们一般要注意键盘 UITextFIEld

一个UITextFIEld对象在你的界面上显示一个可编辑的文本区域。你使用的文本字段收集文本输入使用屏幕键盘的用户。键盘是可配置的许多不同类型的输入,如纯文本,电子邮件,数字等。文本字段使用目标 *** 作机制和一个委托对象来报告编辑过程中所做的更改。


UITextFIEld,系统给我们提供了四中样式:


在使用上,我们可以使用系统提供样式,也可以自己定义。在使用上我们一般要注意键盘keyboardTypereturnKeyType这两个属性,因为这两个在用户体验过伤会有比较大的影响。比如,一个手机号码输入框,我们不设置键盘类型,需要用户去切换,用户就会感觉不爽了。

func initUITextFeild(){        let textFeild = UITextFIEld.init()        self.vIEw.addSubvIEw(textFeild)        textFeild.frame = CGRectMake(60,100,200,30) //UITextFIEld的高度固定是30        textFeild.clearbuttonMode = UITextFIEldviewmode.WhileEditing // 清除按钮模式        textFeild.borderStyle = UITextborderStyle.line // 边框样式        textFeild.secureTextEntry = true // 密文显示        textFeild.placeholder = "我是UITextFIEld" // 设置placeholder        //设置placeholder的字体颜色        textFeild.setValue(UIcolor.bluecolor(),forKeyPath: "placeholderLabel.textcolor")        textFeild.delegate = self // 关联代理        textFeild.returnKeyType = UIReturnKeyType.Done  // 键盘右下角按钮类型        textFeild.keyboardType = UIKeyboardType.namePhonePad // 键盘类型        textFeild.enabled = true // 是否可以编辑  true:可以编辑 false:不可以编辑  默认是true        textFeild.Font = UIFont.systemFontOfSize(15) // 字体大小        textFeild.textcolor = UIcolor.redcolor() // 字体颜色        textFeild.textAlignment = NSTextAlignment.Center // 居中对齐        // 左边图片        let imageVIEw = UIImageVIEw.init(frame: CGRectMake(0,20,20))        imageVIEw.image = UIImage.init(named: "2")        textFeild.leftviewmode = UITextFIEldviewmode.Always        textFeild.leftVIEw = imageVIEw        //        textFeild.inputVIEw // 自定义输入视图,可以是键盘//        textFeild.inputAccessoryVIEw // 自定义副键盘    }
使用UITextFIEld需要关联一个他的代理UITextFIEldDelegate,在实际中,当我们不要他的代理事件的时候,我们可以不关联代理和实现代理方法。

//MARK: UITextFIEldDelegate    func textFIEldShouldBeginEditing(textFIEld: UITextFIEld) -> Bool {        print("textFIEldShouldBeginEditing")        return true   // true:可以编辑 false:不可以编辑 和enabled功能相似    }    func textFIEldDIDBeginEditing(textFIEld: UITextFIEld) {        print("textFIEldDIDBeginEditing")    }    func textFIEldShouldEndEditing(textFIEld: UITextFIEld) -> Bool {        print("textFIEldShouldEndEditing")        return true // true:可以结束编辑 false:不可以结束编辑    }    func textFIEldDIDEndEditing(textFIEld: UITextFIEld) {        print("textFIEldDIDEndEditing")    }    func textFIEldShouldClear(textFIEld: UITextFIEld) -> Bool {        print("textFIEldShouldClear")        return true    }    func textFIEldShouldReturn(textFIEld: UITextFIEld) -> Bool {        if textFIEld.returnKeyType ==  UIReturnKeyType.Done{            print("UIReturnKeyType.Done")            textFIEld.resignFirstResponder() // 收起键盘        }        print("text = \(textFIEld.text)") // 打印输入的数据        return true    }    func textFIEld(textFIEld: UITextFIEld,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool {        print("shouldChangeCharactersInRange")        return true    }
在这里 //MARK: 是Swift里面的一个代码标记方法,类似于OC里面的#program mark -


但是个人觉得,由于不能联想,没有OC里面的Mark好使用。

UITextVIEw(placeholder的制作)

一种文本视图接受并显示多行文本。文本视图支持滚动和文本编辑。您通常使用一个文本视图来显示大量的文本,例如电子邮件信息的主体。


UITextVIEw在使用子和UITextFIEld差不多,很多时候我们需要自己做类似UITextFIEld的placeholder,其实我们只需要知道UITextVIEw的几个代理方法的执行顺序就大概知道怎么做了,这里提供一种做法,还要更好的做法,大家可以自己去尝试。

    func initUITextVIEw(){        let textVIEw = UITextVIEw.init()        textVIEw.frame = CGRectMake(60,160,200)        self.vIEw.addSubvIEw(textVIEw)        textVIEw.layer.borderWIDth = 1 // 设置边框 宽度        textVIEw.layer.bordercolor = UIcolor.graycolor().CGcolor // 设置边框颜色        textVIEw.keyboardType = UIKeyboardType.EmailAddress // 键盘类型        textVIEw.returnKeyType = UIReturnKeyType.Done // 键盘右下键 按钮类型        textVIEw.delegate = self        textVIEw.showsverticalScrollindicator = false // 不显示垂直 滑动线        textVIEw.showsHorizontalScrollindicator = false // 不显示 水平滑动线        //        textVIEw.inputVIEw // 自定义输入视图,可以是键盘//        textVIEw.inputAccessoryVIEw // 自定义副键盘        // 制作placeholder        self.placeholderLabel = UILabel.init() // placeholderLabel是全局属性        self.placeholderLabel.frame = CGRectMake(5,5,20)        self.placeholderLabel.Font = UIFont.systemFontOfSize(13)        self.placeholderLabel.text = "我是placeholder"        textVIEw.addSubvIEw(self.placeholderLabel)        self.placeholderLabel.textcolor = UIcolor.init(colorliteralRed: 72/256,green: 82/256,blue: 93/256,Alpha: 1)    }

UITextVIEw也有几个代理方法:

//MARK: UITextVIEwDelegate    func textVIEwShouldBeginEditing(textVIEw: UITextVIEw) -> Bool {        print("textVIEwShouldBeginEditing")        self.placeholderLabel.hIDden = true // 隐藏        return true    }    func textVIEwDIDBeginEditing(textVIEw: UITextVIEw) {            }    func textVIEwShouldEndEditing(textVIEw: UITextVIEw) -> Bool {        print("textVIEwShouldEndEditing")        return true    }    func textVIEwDIDEndEditing(textVIEw: UITextVIEw) {        if textVIEw.text.isEmpty {            self.placeholderLabel.hIDden = false  // 显示        }        else{            self.placeholderLabel.hIDden = true  // 隐藏        }        print("textVIEwDIDEndEditing")    }    func textVIEwDIDChange(textVIEw: UITextVIEw) {        print("textVIEwDIDChange")    }    func textVIEw(textVIEw: UITextVIEw,shouldChangeTextInRange range: NSRange,replacementText text: String) -> Bool {        print("shouldChangeTextInRange")        if text == "\n"{ // 输入换行符时收起键盘           textVIEw.resignFirstResponder() // 收起键盘        }        return true    }
使用效果图:

总结

以上是内存溢出为你收集整理的Swift UITextField/UITextView(placeholder的制作)全部内容,希望文章能够帮你解决Swift UITextField/UITextView(placeholder的制作)所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1077749.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存