IOS 点击按钮唤醒键盘 swift 版

2020-11-12  本文已影响0人  路边的风景呢

swift 版


protocol keyBoardViewDelegate {

    func uploadMessage(text:String)

}

class FZKeyBoardView: UIView,UITextViewDelegate {

    var delegate:keyBoardViewDelegate?

    var placeholderText:String?{

        didSet{

            placeholderLabel.text = placeholderText

        }

    }

    varbackView:UIView= {

        letbackView =UIView.init(frame:CGRect(x:0, y:0, width:Screen_Width, height:43))

        backView.backgroundColor= .white

        returnbackView

    }()

    vartextView:UITextView= {

        lettextView =UITextView.init(frame:CGRect(x:0, y:0, width:Screen_Width-64, height:43))

        textView.backgroundColor= .white

        textView.font= .systemFont(ofSize:16)

        returntextView

    }()

    varsendButton:UIButton= {

        letsend =UIButton.init(frame:CGRect(x:Screen_Width-64, y:0, width:64, height:43))

        send.backgroundColor = .gray

        send.setTitle("发送", for: .normal)

        send.isUserInteractionEnabled = false

        send.titleLabel!.font= .systemFont(ofSize:16)

        returnsend

    }()

    var emptyButton:UIButton = {

        letsend =UIButton.init(frame:CGRect(x:Screen_Width-64, y:0, width:64, height:43))

        send.backgroundColor = .clear

        returnsend

    }()

    var placeholderLabel:UILabel = {

        letLabel =UILabel.init(frame:CGRect(x:10, y:0, width:Screen_Width-74, height:40))

        Label.textColor= .gray

        returnLabel

    }()

    overrideinit(frame:CGRect) {

        super.init(frame: frame)

        //背景View

        self.addSubview(backView)

        emptyButton.addTarget(self, action:#selector(closeIputView), for: .touchUpInside)

        self.addSubview(emptyButton)

        textView.delegate=self

        textView.inputAccessoryView = UIView.init()

        backView.addSubview(textView)

        backView.addSubview(placeholderLabel)

        sendButton.addTarget(self, action:#selector(sendButtonClick), for: .touchUpInside)

        backView.addSubview(sendButton)

        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShowAction), name: UIResponder.keyboardWillShowNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHidenAction), name: UIResponder.keyboardWillHideNotification, object: nil)

    }

    @objcfunckeyBoardWillShowAction(notification:Notification){

        self.frame=CGRect(x:0, y:0, width:Screen_Width, height:Screen_Height)

        letkeyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey]as?NSValue)?.cgRectValue

        letHeight = keyboardSize?.size.height

        self.backView.frame=CGRect(x:0, y:Screen_Height-43-Height!, width:Screen_Width, height:43)

        self.emptyButton.frame=CGRect(x:0, y:0, width:Screen_Width,height:Screen_Height-43-Height!);

    }

    @objc func keyBoardWillHidenAction(){

        self.frame=CGRect(x:0, y:Screen_Height, width:Screen_Width, height:Screen_Height)

    }

    @objc func closeIputView(){

        self.textView .resignFirstResponder()

    }

    @objc func sendButtonClick(){

        self.delegate?.uploadMessage(text: self.textView.text)

        self.textView.text=""

        self.textView .resignFirstResponder()

        self.sendButton.backgroundColor = .gray

        self.sendButton.isUserInteractionEnabled = false

    }

    functextViewDidChange(_textView:UITextView) {

        iftextView.text.count==0{

            self.placeholderLabel.text = placeholderText

            self.sendButton.backgroundColor = .gray

            self.sendButton.isUserInteractionEnabled = false

        }else{

            self.placeholderLabel.text=""

            self.sendButton.isUserInteractionEnabled = true

            self.sendButton.backgroundColor= .red

        }

    }

    requiredinit?(coder:NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}

上一篇 下一篇

猜你喜欢

热点阅读