UITextView自适应+键盘切换

效果图

textView1.gif

主要解决2个问题:

  • 键盘切换的之后textView父视图frame的改变问题
  • 输入文字时textView高度的变化和父视图frame的变化

切换键盘

这里切换键盘用到的是textView的inputView属性,如果不给注销和出现的方法加动画,键盘改变的回调给父视图的frame会改变两次,出现动画乱跳的问题,这里只能强行解决了一下

    // 切换表情按钮点击
    @objc fileprivate func changeButtonDidClick(_ button: UIButton) {
        changeButton.isSelected = !button.isSelected
        UIView.animate(withDuration: 0.01) { 
            self.textView.resignFirstResponder()
            self.textView.inputView = self.textView.inputView == nil ? self.emojiView : nil
            self.textView.becomeFirstResponder()
        }
    }

监听键盘的出现和消失

// 这里监听键盘的2个方法,其实可以监听键盘frameWillChange
// 键盘将要出现
    @objc fileprivate func keyboardWillShowNotification(notification: Notification) {
        print(notification.userInfo!["UIKeyboardFrameEndUserInfoKey"] ?? false)
        let changeFrame = notification.userInfo!["UIKeyboardFrameEndUserInfoKey"] as! CGRect
        let changeHeight = changeFrame.size.height
        keyboardWillShowCallBack?(changeHeight)
    }
    
    // 键盘将要消失
    @objc fileprivate func keyboardWillHideNotification(notification: Notification) {
        print(notification.userInfo!["UIKeyboardFrameEndUserInfoKey"] ?? false)
        let changeFrame = notification.userInfo!["UIKeyboardFrameEndUserInfoKey"] as! CGRect
        let changeHeight = changeFrame.size.height
        keyboardWillHideCallBack?(changeHeight)
    }

键盘回调方法

        var inputViewHeight: CGFloat = 50 // 记录textView的高度
        var inputViewY: CGFloat = appHeight - inputViewHeight // 记录textView的Y
        var letInputViewY: CGFloat = inputViewY
        
        let inputView = InputView(frame: CGRect(x: 0, y: inputViewY, width: appWidth, height: inputViewHeight))
        
// 键盘将要出现
        inputView.keyboardWillShowCallBack = { keyboardHeight in
            inputViewY = appHeight - keyboardHeight - inputViewHeight
            letInputViewY = appHeight - keyboardHeight
            inputView.frame = CGRect(x: 0, y: inputViewY, width: appWidth, height: inputViewHeight)
        }
// 键盘将要消失
        inputView.keyboardWillHideCallBack = { keyboardHeight in
            inputViewY = appHeight - inputViewHeight
            letInputViewY = appHeight
            inputView.frame = CGRect(x: 0, y: inputViewY, width: appWidth, height: inputViewHeight)
        }
// textView高度改变回调
        inputView.textViewHeightChanged = { changeHeight in
            inputViewHeight = changeHeight + 10
            inputView.frame = CGRect(x: 0, y: letInputViewY - inputViewHeight, width: appWidth, height: changeHeight + 10)
        }

letInputViewY 是为了记录当键盘切换时Y的改变量,当textView行高发生变化的时候计算偏移的Y
inputViewHeight 是为了记录textView改变的高度,当切换键盘时计算偏移的Y

解决textView自适应问题

核心代码还是利用textView.sizeThatFits方法计算textView的新高度然后重新赋值,并回调给父视图改变父视图的Y即可,这里改变了一下textView的attributes属性来调整行间距,当发送emoji表情的时候会导致emoji的高度高于计算的大小,emoji的下半边会被遮盖,demo没有实现表情键盘

func textViewDidChange(_ textView: UITextView) {
        
        let maxHeight: CGFloat = 90.0;
        let width = textView.frame.size.width;
        let newSize = textView.sizeThatFits(CGSize(width: width, height: CGFloat(MAXFLOAT)))
        let textHeight = CGFloat(newSize.height)
        
        // 修改TextView的行距和attributes
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 5
        let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15), NSAttributedStringKey.paragraphStyle: paragraphStyle]
        textView.attributedText = NSAttributedString(string: textView.text, attributes: attributes)
        
        if textHeight <= maxHeight {
            textView.isScrollEnabled = false
            textView.bounds.size.height = textHeight >= 40 ? textHeight : 40 // 高度大于40的时候再高边textView高度
            textViewHeightChanged?(textView.bounds.size.height)
        }else
        {
            textView.isScrollEnabled = true
            textView.bounds.size.height = maxHeight
            textViewHeightChanged?(textView.bounds.size.height)
        }
    }

主要为了写个demo练习一下swift,并且养成写简书的习惯,刚就业不到半年的小白,借助简书记录自己的学习之路,代码质量不是很高😝
demo:https://github.com/qq421680227/textViewSizeFit

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,246评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,790评论 25 709
  • 磨蹭一个月,终于把《越狱》四季看完了。每次总是会提前看底下的评论,知道一点后面的剧情,这是一部没法让人快...
    小雨正在奔跑9299阅读 10,657评论 0 0
  • 热播剧《那年花开月正圆》即将接近尾声。该剧以陕西省泾阳县安吴堡吴氏家族的史实为背景,讲述了清末出身民间的陕西女首富...
    杜小美阅读 6,360评论 0 0
  • 日前读报,一个年仅二十几的女子因男友另结新欢,承受不了情感重创,用几片断肠草叶子结束生命,经抢救无效身亡,留下年迈...
    悠悠心旅阅读 2,578评论 0 0