|
@@ -16,7 +16,7 @@ class TSChatInputFullScreenVC: TSBaseVC, UITextViewDelegate {
|
|
|
lazy var InputBarView:UIView = {
|
|
|
let InputBarView = UIView()
|
|
|
InputBarView.backgroundColor = "#222222".uiColor
|
|
|
- InputBarView.cornerRadius = 16.0
|
|
|
+ InputBarView.cornerRadius = 28.0
|
|
|
return InputBarView
|
|
|
}()
|
|
|
|
|
@@ -27,6 +27,7 @@ class TSChatInputFullScreenVC: TSBaseVC, UITextViewDelegate {
|
|
|
sendComplete?([string])
|
|
|
}
|
|
|
textView.text = ""
|
|
|
+ dismiss(animated: true)
|
|
|
}
|
|
|
return sendBtn
|
|
|
}()
|
|
@@ -42,19 +43,22 @@ class TSChatInputFullScreenVC: TSBaseVC, UITextViewDelegate {
|
|
|
|
|
|
private let minHeight: CGFloat = 24
|
|
|
private let maxHeight: CGFloat = 154
|
|
|
- lazy var textView: UITextView = {
|
|
|
- let textView = UITextView()
|
|
|
+ lazy var textView: TSCustomTextView = {
|
|
|
+ let textView = TSCustomTextView()
|
|
|
textView.backgroundColor = .clear
|
|
|
textView.textColor = .white
|
|
|
textView.delegate = self
|
|
|
textView.font = .font(size: 16)
|
|
|
textView.clipsToBounds = true
|
|
|
- textView.isScrollEnabled = true
|
|
|
+ textView.isScrollEnabled = false
|
|
|
textView.tintColor = .themeColor
|
|
|
- textView.textContainerInset = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
|
|
|
+ textView.returnKeyType = .send
|
|
|
+ textView.placeholder = "Type all necessary details".localized
|
|
|
+ textView.placeholderColor = .white.withAlphaComponent(0.4)
|
|
|
+ textView.placeholderLabel.font = .font(size: 16.0)
|
|
|
+ textView.textInsets = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
|
|
|
return textView
|
|
|
}()
|
|
|
-
|
|
|
|
|
|
override func createView() {
|
|
|
setNavBarViewHidden(true)
|
|
@@ -93,43 +97,94 @@ class TSChatInputFullScreenVC: TSBaseVC, UITextViewDelegate {
|
|
|
make.width.equalTo(24)
|
|
|
make.height.equalTo(24)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
override func dealThings() {
|
|
|
- // 监听文本变化事件
|
|
|
-// NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: UITextView.textDidChangeNotification, object: textView)
|
|
|
textView.text = text
|
|
|
+
|
|
|
+ let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickView))
|
|
|
+ tapGesture.cancelsTouchesInView = false
|
|
|
+ InputBarView.addGestureRecognizer(tapGesture)
|
|
|
+
|
|
|
+ // 监听文本变化事件
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: UITextView.textDidChangeNotification, object: textView)
|
|
|
+
|
|
|
+ // 监听键盘事件
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
|
|
}
|
|
|
-//
|
|
|
-//
|
|
|
-// @objc private func textDidChange() {
|
|
|
-// // 计算输入框的内容高度
|
|
|
-// let sizeToFit = CGSize(width: textView.bounds.width, height: .greatestFiniteMagnitude)
|
|
|
-// let estimatedSize = textView.sizeThatFits(sizeToFit)
|
|
|
-//
|
|
|
-// // 根据内容高度调整输入框的高度
|
|
|
-// var newHeight = estimatedSize.height
|
|
|
-// if newHeight < minHeight {
|
|
|
-// newHeight = minHeight
|
|
|
-// } else if newHeight > maxHeight {
|
|
|
-// newHeight = maxHeight
|
|
|
-// textView.isScrollEnabled = true
|
|
|
-// } else {
|
|
|
-// textView.isScrollEnabled = false
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 更新输入框的高度
|
|
|
-// textView.snp.updateConstraints { make in
|
|
|
-// make.height.equalTo(newHeight)
|
|
|
-// }
|
|
|
-// }
|
|
|
|
|
|
+ @objc func clickView(){
|
|
|
+ view.endEditing(true)
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc private func textDidChange() {
|
|
|
+ sendEnabled(enabled: textView.text.count > 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ func sendEnabled(enabled:Bool){
|
|
|
+ sendBtn.isEnabled = enabled
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func keyboardWillShow(_ notification: Notification) {
|
|
|
+
|
|
|
+ guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
|
|
|
+ let animationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let keyboardHeight = keyboardFrame.height
|
|
|
+ let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
|
|
|
+
|
|
|
+ UIView.animate(withDuration: animationDuration) {
|
|
|
+ self.textView.contentInset = contentInset
|
|
|
+ self.textView.scrollIndicatorInsets = contentInset
|
|
|
+ }
|
|
|
+
|
|
|
+ UIView.animate(withDuration: animationDuration+0.8) {
|
|
|
+ self.sendBtn.snp.updateConstraints { make in
|
|
|
+ make.bottom.equalTo(-keyboardHeight + k_Height_safeAreaInsetsBottom())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func keyboardWillHide(_ notification: Notification) {
|
|
|
+ guard let animationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let contentInset = UIEdgeInsets.zero
|
|
|
+ UIView.animate(withDuration: animationDuration) {
|
|
|
+ self.textView.contentInset = contentInset
|
|
|
+ self.textView.scrollIndicatorInsets = contentInset
|
|
|
+
|
|
|
+ self.sendBtn.snp.updateConstraints { make in
|
|
|
+ make.bottom.equalTo(-16)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
deinit {
|
|
|
// 移除通知监听
|
|
|
NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: textView)
|
|
|
}
|
|
|
-
|
|
|
+}
|
|
|
|
|
|
+extension TSChatInputFullScreenVC {
|
|
|
+
|
|
|
+ // 实现 UITextViewDelegate 协议方法,控制 return 键行为
|
|
|
+ func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
+ if text == "\n" {
|
|
|
+ // 当输入为换行符(即按下 return 键)时,执行相应操作
|
|
|
+ if let string = textView.text {
|
|
|
+ sendComplete?([string])
|
|
|
+ }
|
|
|
+ dismiss(animated: true)
|
|
|
+ textView.text = ""
|
|
|
+ textView.resignFirstResponder()
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
}
|