美文网首页
Swift-UIKit-UITextField

Swift-UIKit-UITextField

作者: Vicent_Z | 来源:发表于2021-04-22 10:03 被阅读0次

1.描述

class UITextField : UIControl

官方描述:

一个用于展示用户界面上,可编辑文本区域的对象(An object that displays an editable text area in your interface.)

文本输入框用于接收用户界面的文本输入,键盘可以是多种类型比如:文本,email,数字等等.还可以通过target-action机制进行监听文本变化.

而且文本框还可以添加可覆盖视图,可以是自定比的视图,比如系统的清除按键.

UITextField的所有监听事件都是通过UITextFieldDelegate代理来实现的.

当用户点击一个text field时候,这个field变回自动成为第一响应者,并且自动弹出键盘,我们也可以强制的通过调用某一个field的becomeFirstResponder()方法指定其成为第一响应者.

键盘有时候会这当我们界面,这时候我们需要通过键盘相关通知进行修改界面才能解决此问题.

默认键盘是当前语言的标准键盘,我们也可以指定一些特殊的键盘:数字,URLS,email,地址等.

键盘常用的相关的通知:

  • keyboardWillShowNotification
  • keyboardDidShowNotification
  • keboardWillHideNotification
  • keyboardDidHideNotification
  • keyboardWillChangeFrameNotification
  • keyboardDidChangeFrameNotification

每个通知都包含了一个包含了键盘大小的userInfo字典用来方便我们控制视图的变换,以防键盘遮挡视图内容.

我们也可以通过field的textField(_:shouldChangeCharactersIn:replacementString:)代理方法来验证和格式化用户输入的内容.

覆盖制图(Overlay Views)

我们可以通过制定我们自定义的button视图来作为field的覆盖视图.通过leftView(rightView)和leftViewMode(rightViewMode)属性来操作.
系统也提供我们一个清除内容的覆盖视图简单方法,通过clearButtonMode方法

验证文本和管理编辑进程

文本框可以通过代理(UITextField Delegate)对象管理用户的输入校验和监听用的编辑进程.

2.代码示例

        let tf = UITextField()
        self.view.addSubview(tf)
        tf.translatesAutoresizingMaskIntoConstraints = false
        self.view.translatesAutoresizingMaskIntoConstraints = false
        let constraintArrayH = NSLayoutConstraint.constraints(withVisualFormat: "H:|-100-[tf(200)]", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["tf":tf])
        let constraintArrayV = NSLayoutConstraint.constraints(withVisualFormat: "V:|-100-[tf]", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["tf":tf])
        self.view.addConstraints(constraintArrayH)
        self.view.addConstraints(constraintArrayV)
        
        //文本内容
//        tf.text = "Hello UITextField Hello UITextField"
        //背景色
//        tf.backgroundColor = UIColor.green
        //文字颜色
        tf.textColor = UIColor.systemBlue
        //字体大小
        tf.font = UIFont.systemFont(ofSize: 15)
        //文本布局
//        tf.textAlignment = NSTextAlignment.center
        //text为空时候的默认占位内容
        tf.placeholder = "Please input your content"

        //边框样式
        tf.borderStyle = UITextField.BorderStyle.roundedRect
        //删除按钮显示模式
        tf.clearButtonMode = UITextField.ViewMode.always
        //文本展示不开时候的最小字体大小
        tf.adjustsFontSizeToFitWidth = true
        tf.minimumFontSize = 8.0
        
        /**键盘属性相关**/
        //自动大写类型
        tf.autocapitalizationType = UITextAutocapitalizationType.sentences
        //自动关联正确词汇
        tf.autocorrectionType = UITextAutocorrectionType.no
        //键盘类型
//        tf.keyboardType = UIKeyboardType.numberPad
        //键盘主体样式
//        tf.keyboardAppearance = UIKeyboardAppearance.dark
        //返回键按钮样式
        tf.returnKeyType = UIReturnKeyType.google

3.总结

UIView是一个最基础常见的视图类,可以参考文档进行深入学习:UIKit->Views and Controls->UIView

相关文章

  • Swift-UIKit-UITextField

    1.描述 官方描述: 一个用于展示用户界面上,可编辑文本区域的对象(An object that displays...

网友评论

      本文标题:Swift-UIKit-UITextField

      本文链接:https://www.haomeiwen.com/subject/bfcrqltx.html