美文网首页
swift-文本输入框(TextFiled)

swift-文本输入框(TextFiled)

作者: 寒丶酥 | 来源:发表于2019-04-15 23:08 被阅读0次

    1.Textfiled的创建

    let textField:UITextField =  UITextField(frame: CGRect(x: 20, y: 100, width: 240, height: 40))
    self.view.addSubview(textField)
    

    2.Textfiled的基础应用

    //设置输入框风格为line
    textField.borderStyle = UITextField.BorderStyle.line
    //设置输入框风格为bezel
    textField.borderStyle = UITextField.BorderStyle.bezel
    //设置输入框的提示文字
    textField.placeholder = "请输入文字"
    //设置输入的文字颜色
    textField.textColor = UIColor.blue
    //设置文字的字体
    textField.font = UIFont.italicSystemFont(ofSize: 20)
    //设置文字的对齐方式
    textField.textAlignment = NSTextAlignment.center
    //textField.backgroundColor = UIColor.red
    //是否每次进入编辑状态时都清空输入框中的文字
    textField.clearsOnBeginEditing =  true
    //是否字体大小自适应
    textField.adjustsFontSizeToFitWidth = true
    //设置输入框为无效
    //textField.isEnabled = false
    //设置输入框的左视图
    let viewLeft:UIView =  UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    let viewRight: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    viewLeft.backgroundColor = UIColor.red
    viewRight.backgroundColor = UIColor.blue
    textField.leftView = viewLeft
    textField.rightView = viewRight
    //设置左右视图的显示模式
    textField.leftViewMode = UITextField.ViewMode.always
    textField.rightViewMode = UITextField.ViewMode.always
    //设置弹出的交互键盘
    let board:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
    board.backgroundColor = UIColor.orange
    //textField.inputView = board
    //设置副键盘视图
    textField.inputAccessoryView = board
    //设置代理
    textField.delegate = self
    //设置文本框删除按钮的显示模式
    textField.clearButtonMode = UITextField.ViewMode.always
    

    3.Textfiled的方法函数

    //当输入改变时调用的方法
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print(string)
        return true
    }
    //textField已经进入编辑状态时调用的方法
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print("didBegin")
    }
    //结束编辑状态时调用的方法
    func textFieldDidEndEditing(_ textField: UITextField) {
        print("didEnd")
    }
    //将要进入编辑状态时调用的方法
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        print("shouldBegin")
        return true
    }
    //将要结束编辑状态时调用的方法
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        print("shouldEnd")
        return true
    }
    //当点击删除按钮时触发的方法
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        return false
    }
    //点击return键触发的方法
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        textField.resignFirstResponder()
        }
    

    相关文章

      网友评论

          本文标题:swift-文本输入框(TextFiled)

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