想微信一样的输入框textview 一次可以输入多行 textView的placeholder提示框


class MessageTextView: UIView,UITextViewDelegate {

    

    var commnetToleft:CGFloat = 16

    var CancleLabToRight:CGFloat = 8

    var commnetToTop:CGFloat = 10

    var cancleLabWidth:CGFloat = 52

    var cancleLabHeight:CGFloat = 30

    var cancleBtnFont:CGFloatWordSize.word_2.rawValue

    var isallowMessage:Bool = false

    var commentView:UITextView!

    var placeHolderLab:UILabel!

    var cancleBtn:UIButton!

    var comm_new:String!


    //判断输入框中 有没有数据

    var isHaveData:Bool = false

    

    var replayData:[MessageModle] = []


   //在textview did change 里写入这个回调 ,在键盘监听事件实现

    var refreshFrame:(()->Void)?

    

   //点击取消按钮的回调 bool类型是判断有没有数据

    var cancleBtnClick:((Bool)->Void)?

    

  //初始化不多说

    override init(frame: CGRect) {

        super.init(frame: frame)

        initView()

        self.backgroundColor = ColorValue.color_10

    }

    

    func initView(){

        if AppDelegate.phoneType == PhoneType.iphone_6P{

            commnetToTop = 10

            commnetToleft = 16

            CancleLabToRight = 8

            cancleLabWidth = 52

            cancleLabHeight = 30

            cancleBtnFont = WordSize.word_2.rawValue

        }

        else if AppDelegate.phoneType == PhoneType.iphone_6{

            commnetToleft = 16

            CancleLabToRight = 8

            commnetToTop = 10

            cancleLabWidth = 52

            cancleLabHeight = 30

            cancleBtnFont = WordSize.word_2.rawValue

        }

        

        

        cancleBtn = UIButton()

        cancleBtn.frame = CGRectMake(self.frame.width - CancleLabToRight - cancleLabWidth, commnetToTop, cancleLabWidth, cancleLabHeight)

        cancleBtn.backgroundColor = ColorValue.color_4

        cancleBtn.layer.masksToBounds = true

        cancleBtn.layer.cornerRadius = 4

        cancleBtn.setTitle("取消", forState: UIControlState.Normal)

        cancleBtn.titleLabel?.font = UIFont.systemFontOfSize(cancleBtnFont)

        cancleBtn.addTarget(self, action: #selector(MessageTextView.btnSubmit(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        cancleBtn.setTitleColor(ColorValue.makeColor(68, g: 68, b: 68, a: 1), forState: UIControlState.Normal)

       

        

        

        commentView = UITextView()

        commentView.frame = CGRectMake(commnetToleft, commnetToTop, self.frame.width - CancleLabToRight - commnetToleft - cancleLabWidth - commnetToleft, cancleLabHeight)

        commentView.font = UIFont.systemFontOfSize(WordSize.word_2.rawValue)

        commentView.backgroundColor = ColorValue.color_4

        commentView.layer.masksToBounds = true

        commentView.layer.cornerRadius = 4

        commentView.layer.borderWidth = 1

        commentView.delegate = self

        commentView.layer.borderColor = ColorValue.makeColor(223, g: 223, b: 223, a: 1.0).CGColor

        

        

        placeHolderLab = UILabel()

        placeHolderLab.text = "新建留言..."

        placeHolderLab.textColor = ColorValue.makeColor(128, g: 128, b: 128, a: 1.0)

        placeHolderLab.font = UIFont.systemFontOfSize(WordSize.word_2.rawValue)

        placeHolderLab.sizeToFit()

        placeHolderLab.frame = CGRectMake(8,9,placeHolderLab.frame.width , placeHolderLab.frame.height)

        placeHolderLab.hidden = false

        

        commentView.addSubview(placeHolderLab)

        self.addSubview(cancleBtn)

        self.addSubview(commentView)

    }

    

   // 通过bool类型 来判断是提交还是 取消

    func RefreshView(isHaveData:Bool){

        if isHaveData{

        cancleBtn.setTitle("发送", forState: UIControlState.Normal)

        cancleBtn.backgroundColor = ColorValue.color_9

        cancleBtn.setTitleColor(ColorValue.color_4,forState: UIControlState.Normal)

        placeHolderLab.hidden = true

        }

        else{

        cancleBtn.setTitle("取消", forState: UIControlState.Normal)

        cancleBtn.backgroundColor = ColorValue.color_4

        cancleBtn.setTitleColor(ColorValue.makeColor(68, g: 68, b: 68, a: 1), forState: UIControlState.Normal)

        }

    }

    

   //重置frame比如在 评论完后 或者键盘消失文字为 “” 时  此时调用这个方法

    func RestroreFrame(){

         commentView.frame = CGRectMake(commnetToleft, commnetToTop, self.frame.width - CancleLabToRight - commnetToleft - cancleLabWidth - commnetToleft, cancleLabHeight)

    }

    

    //代理方法

    func textViewDidChange(textView: UITextView) {

        

        //允许最高的高度的行数

        let maxHeight:CGFloat = 72

        //frame设置

        let fram = textView.frame

        //自己设置的size

        let constraintSize = CGSizeMake(fram.size.width,maxHeight)

        //自适应的

        var siz = textView.sizeThatFits(constraintSize)

        //如果textview 大于设置的maxheight,这个heigtht 就跟maxheight相同,且可以滚动

        if (siz.height >= maxHeight){

            siz.height = maxHeight

            

        }

            //否则不可以滚动

        else{

            

        }

        //在设置textViewframe

        textView.frame = CGRectMake(fram.origin.x,fram.origin.y,fram.size.width,siz.height)

        refreshFrame?()

        

       

       let text =  (textView.text as NSString).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

        

        if  text == ""{

            isHaveData = false

            isallowMessage = false

            placeHolderLab.hidden = false

        }

        else {

            placeHolderLab.hidden = true

            isHaveData = true

        }

        RefreshView(isHaveData)

    }

    

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

        if let str = comm_new {

            //判断输入的值是否为删除键

            if (range.location < str.length && range.length == 1 && text.length == 0)

            {

                textView.text = NSString(string: textView.text!).substringFromIndex(((str).length))

                isallowMessage = false

                comm_new = nil

                placeHolderLab.hidden = false

                RefreshView(false)

            }


        }

        return true

    }

    

    func btnSubmit(sender:UIButton){

      //true 是 提交  false 是取消

        cancleBtnClick?(isHaveData)

    }

    

    required init?(coder aDecoder: NSCoder) {

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

    }


}



  //controller添加评论的textview

    func addCommentView(){

        commentView = MessageTextView(frame:CGRectMake(0, self.view.frame.height - 50, self.view.frame.width, 50))

        commentView.cancleBtnClick = {[weak self] hadData in

            if hadData {

                //有数据提交评论

                提交

            }

            else {

                //返回界面或者键盘消失 暂时键盘消失

                self?.commentView.commentView.resignFirstResponder()

            }

        }

     

        self.view.addSubview(commentView)

    }



//这是高度随着键盘变化 

//键盘事件的监听

    func addkeyboardWillchange(){

        NSNotificationCenter.defaultCenter().removeObserver(self)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Messagefunctiontips.CommentViewChange), name: UIKeyboardWillChangeFrameNotification, object: nil)

        

    }

    func CommentViewChange(sender:NSNotification){

        let _ = UITextInputMode().primaryLanguage

        let userInfo: NSDictionary = sender.userInfo!

        let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue

        let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue

        

        // let tableViewHeight = keyboardFrame.origin.y - 64 - 50   // tableView高度

        

        if let contentView = ctr.commentView {

            UIView.animateWithDuration(duration, animations: {[weak self] () -> Void in

                

                if keyboardFrame.origin.y > self!.ctr!.view.frame.height {

                    //键盘Y值大于控制器view高度

                 

                    contentView.frame.origin.y = self!.ctr!.view.frame.height - contentView.frame.height

                    

                    

                }else {

//每次textviewdidchange 都刷新他的frame

                    self?.ctr.commentView.refreshFrame = {[weak self] in

                        self?.ctr.commentView.frame = CGRectMake(0, (self?.ctr.view.frame.height)! - 20 - self!.ctr.commentView.commentView.frame.height,(self?.ctr.view.frame.width)! , self!.ctr.commentView.commentView.frame.height + 20)

                        contentView.frame.origin.y = keyboardFrame.origin.y - contentView.frame.height

                    }

                    

                    contentView.frame.origin.y = keyboardFrame.origin.y - contentView.frame.height

                }

                

            }) { (flag) -> Void in

                if flag {

                    

                }

            }

        }

    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值