0
点赞
收藏
分享

微信扫一扫

iOS开发之UITextFiled的使用(swift)

JamFF 2022-03-30 阅读 78
ios
//MARK: UITextFiled
class TextViewController: UIViewController, UITextFieldDelegate {
    let textFiled = UITextField()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        //设置textfiled的大小和位置
        textFiled.frame= CGRect(x: 10, y: 60, width: 200, height: 30)
        //设置边框样式
        textFiled.borderStyle = UITextField.BorderStyle.roundedRect  //样式已经改名
        //设置背景颜色
        textFiled.backgroundColor = .green
        //设置提示文本内容
        textFiled.placeholder= "请输入用户名"
        //设置输入安全文本(输入密码)
        textFiled.isSecureTextEntry = true
        //设置文字样式和大小
        textFiled.font= UIFont(name: " ", size: 20)
        //设置文字颜色
        textFiled.textColor= .red
        //设置文本对齐方式
        textFiled.textAlignment = .center
        //设置进入编辑状态后是否清空输入框中的文字
        textFiled.clearsOnBeginEditing = true
        //设置字体大小自适应
        textFiled.adjustsFontSizeToFitWidth = true
        //设置输入框有效和无效时的背景图片(受输入框的边框样式影响)
        textFiled.background= UIImage(named: "2")
        textFiled.disabledBackground = UIImage(named: "1")
        //设置删除按钮的触发方式
        textFiled.clearButtonMode = UITextField.ViewMode.always
        //设置输入框的状态为无效
        textFiled.isEnabled= false
        //设置键盘样式
        textFiled.keyboardType = UIKeyboardType.emailAddress
        //设置键盘外观
        textFiled.keyboardAppearance = UIKeyboardAppearance.alert
        //设置输入框的左右视图
        letviewLeft = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 30))
        letviewRight = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 30))
        viewLeft.backgroundColor= .brown
        viewRight.backgroundColor= .purple
        textFiled.leftView= viewLeft
        textFiled.rightView= viewRight
        //设置输入框的显示模式
        textFiled.leftViewMode = UITextField.ViewMode.always
        textFiled.rightViewMode = UITextField.ViewMode.always
        //设置弹出的交互键盘
        letboard = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
        board.backgroundColor= .red
        textFiled.inputAccessoryView = board
        //设置代理
        textFiled.delegate= self
        view.addSubview(textFiled)
    }
    overridefunctouchesBegan(_touches: Set<UITouch>, withevent: UIEvent?) {
        textFiled.resignFirstResponder()
    }
    
    //输入内容改变调用的方法
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print(string)
        return true
    }
    
    //已经进入编辑状态调用的方法
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print("textFieldDidBeginEditing")
    }
    
    //结束编辑状态调用的方法
    func textFieldDidEndEditing(_ textField: UITextField) {
        print("textFieldDidEndEditing")
    }
    //将要进入编辑状态调用的方法
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        print("textFieldShouldBeginEditing")
        return true
    }
    
    //将要结束编辑状态调用的方法
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        print("textFieldShouldEndEditing")
        return true
    }
    
    //点击删除按钮触发的方法
    functextFieldShouldClear(_textField: UITextField) -> Bool{
        print("点击了删除按钮")
        return true
    }
    
    //点击return键触发的方法
    functextFieldShouldReturn(_textField: UITextField) -> Bool{
        print("点击了return按钮")
        return true
    }
}
举报

相关推荐

0 条评论