效果图
主要解决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