Commit 74fe5f06 authored by Steven杜宇's avatar Steven杜宇

// AI

parent 8f1d6f19
...@@ -10,6 +10,40 @@ import UIKit ...@@ -10,6 +10,40 @@ import UIKit
class YHAutoTextView: UITextView, UITextViewDelegate { class YHAutoTextView: UITextView, UITextViewDelegate {
private var calculateHeight: CGFloat = 0.0
var isShowKeyboard: Bool = false {
didSet {
if !isShowKeyboard {
self.snp.updateConstraints { make in
make.top.equalTo(5)
make.height.equalTo(36)
make.bottom.equalTo(-5)
}
} else {
var realHeight = self.calculateHeight
if self.calculateHeight > maxHeight {
height = maxHeight
self.snp.updateConstraints { make in
make.top.equalTo(11)
make.height.equalTo(realHeight)
make.bottom.equalTo(-11)
}
} else {
self.snp.updateConstraints { make in
make.top.equalTo(11-Self.verticalGap)
make.height.equalTo(realHeight)
make.bottom.equalTo(-(11-Self.verticalGap))
}
}
}
self.setNeedsLayout()
self.layoutIfNeeded()
}
}
var textChange:((String)->())? var textChange:((String)->())?
override open var text: String! { override open var text: String! {
...@@ -28,25 +62,27 @@ class YHAutoTextView: UITextView, UITextViewDelegate { ...@@ -28,25 +62,27 @@ class YHAutoTextView: UITextView, UITextViewDelegate {
} }
} }
let placeholderLabel: UILabel = { lazy var placeholderLabel: UILabel = {
let label = UILabel() let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.init(hex: 0xB3C8E9) label.textColor = UIColor.init(hex: 0xB3C8E9)
label.font = UIFont.PFSC_R(ofSize: 14) label.font = UIFont.PFSC_R(ofSize: 14)
return label return label
}() }()
override init(frame: CGRect, textContainer: NSTextContainer?) { override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer) super.init(frame: frame, textContainer: textContainer)
self.font = .PFSC_R(ofSize: 14) self.font = .PFSC_R(ofSize: 14)
delegate = self delegate = self
isScrollEnabled = false // 禁止滚动 isScrollEnabled = false // 禁止滚动
self.addSubview(placeholderLabel) self.addSubview(placeholderLabel)
placeholderLabel.snp.makeConstraints { make in placeholderLabel.snp.makeConstraints { make in
make.center.equalToSuperview() make.center.equalToSuperview()
make.left.equalTo(5) make.left.equalTo(5)
make.right.equalTo(-5) make.right.equalTo(-5)
} }
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
...@@ -72,8 +108,10 @@ class YHAutoTextView: UITextView, UITextViewDelegate { ...@@ -72,8 +108,10 @@ class YHAutoTextView: UITextView, UITextViewDelegate {
let size = sizeThatFits(CGSize(width: frame.width, height: .greatestFiniteMagnitude)) let size = sizeThatFits(CGSize(width: frame.width, height: .greatestFiniteMagnitude))
var height = size.height var height = size.height
self.calculateHeight = height
isScrollEnabled = height > maxHeight isScrollEnabled = height > maxHeight
if height > maxHeight { if height > maxHeight {
height = maxHeight height = maxHeight
self.snp.updateConstraints { make in self.snp.updateConstraints { make in
......
...@@ -93,6 +93,8 @@ class YHAITextInputView: UIView { ...@@ -93,6 +93,8 @@ class YHAITextInputView: UIView {
textView.text = "" textView.text = ""
textView.isScrollEnabled = false textView.isScrollEnabled = false
self.endEditing(true) self.endEditing(true)
textLabel.isHidden = true
textView.isHidden = false
} }
@objc func didStopSendBtnClicked() { @objc func didStopSendBtnClicked() {
...@@ -103,10 +105,12 @@ class YHAITextInputView: UIView { ...@@ -103,10 +105,12 @@ class YHAITextInputView: UIView {
let v = YHAutoTextView() let v = YHAutoTextView()
v.backgroundColor = .clear v.backgroundColor = .clear
v.font = .PFSC_R(ofSize: 14) v.font = .PFSC_R(ofSize: 14)
v.textColor = .mainTextColor
v.placeHolder = "有什么问题尽管问我" v.placeHolder = "有什么问题尽管问我"
v.textChange = { v.textChange = {
[weak self] text in [weak self] text in
guard let self = self else { return } guard let self = self else { return }
self.textLabel.text = text
if status != .loading { if status != .loading {
status = text.count > 0 ? .enableSend : .disableSend status = text.count > 0 ? .enableSend : .disableSend
} }
...@@ -114,6 +118,22 @@ class YHAITextInputView: UIView { ...@@ -114,6 +118,22 @@ class YHAITextInputView: UIView {
return v return v
}() }()
lazy var textLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = .mainTextColor
label.font = .PFSC_R(ofSize: 14)
label.isHidden = true
label.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(tapText))
label.addGestureRecognizer(tap)
return label
}()
@objc func tapText() {
textView.becomeFirstResponder()
}
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
...@@ -154,6 +174,7 @@ class YHAITextInputView: UIView { ...@@ -154,6 +174,7 @@ class YHAITextInputView: UIView {
contentView.addSubview(sendBtn) contentView.addSubview(sendBtn)
contentView.addSubview(loadingImgView) contentView.addSubview(loadingImgView)
contentView.addSubview(textView) contentView.addSubview(textView)
contentView.addSubview(textLabel)
status = .disableSend status = .disableSend
whiteView.snp.makeConstraints { make in whiteView.snp.makeConstraints { make in
...@@ -189,6 +210,12 @@ class YHAITextInputView: UIView { ...@@ -189,6 +210,12 @@ class YHAITextInputView: UIView {
make.bottom.equalTo(-(11-YHAutoTextView.verticalGap)) make.bottom.equalTo(-(11-YHAutoTextView.verticalGap))
make.right.equalTo(sendBtn.snp.left).offset(-5) make.right.equalTo(sendBtn.snp.left).offset(-5)
} }
textLabel.snp.makeConstraints { make in
make.center.equalToSuperview()
make.left.equalTo(textView).offset(5)
make.right.equalTo(textView).offset(-5)
}
addKeyBoardNotify() addKeyBoardNotify()
} }
...@@ -203,7 +230,17 @@ class YHAITextInputView: UIView { ...@@ -203,7 +230,17 @@ class YHAITextInputView: UIView {
let isKeyboardShow = notification.name == UIResponder.keyboardWillShowNotification let isKeyboardShow = notification.name == UIResponder.keyboardWillShowNotification
let bottomMargin = (isKeyboardShow ? -keyboardFrame.height : 0) let bottomMargin = (isKeyboardShow ? -keyboardFrame.height : 0)
if isKeyboardShow {
textLabel.isHidden = true
textView.isHidden = false
} else {
textLabel.isHidden = textView.text.isEmpty
textView.isHidden = !textView.text.isEmpty
}
textLabel.text = textView.text
textView.isShowKeyboard = isKeyboardShow
contentView.snp.updateConstraints { make in contentView.snp.updateConstraints { make in
make.bottom.equalTo(-10-(isKeyboardShow ? 0.0 : k_Height_safeAreaInsetsBottom())) make.bottom.equalTo(-10-(isKeyboardShow ? 0.0 : k_Height_safeAreaInsetsBottom()))
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment