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

// AI

parent 8f1d6f19
......@@ -10,6 +10,40 @@ import UIKit
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)->())?
override open var text: String! {
......@@ -28,25 +62,27 @@ class YHAutoTextView: UITextView, UITextViewDelegate {
}
}
let placeholderLabel: UILabel = {
lazy var placeholderLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.init(hex: 0xB3C8E9)
label.font = UIFont.PFSC_R(ofSize: 14)
return label
}()
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
self.font = .PFSC_R(ofSize: 14)
delegate = self
isScrollEnabled = false // 禁止滚动
self.addSubview(placeholderLabel)
placeholderLabel.snp.makeConstraints { make in
make.center.equalToSuperview()
make.left.equalTo(5)
make.right.equalTo(-5)
}
}
required init?(coder: NSCoder) {
......@@ -72,8 +108,10 @@ class YHAutoTextView: UITextView, UITextViewDelegate {
let size = sizeThatFits(CGSize(width: frame.width, height: .greatestFiniteMagnitude))
var height = size.height
self.calculateHeight = height
isScrollEnabled = height > maxHeight
if height > maxHeight {
height = maxHeight
self.snp.updateConstraints { make in
......
......@@ -93,6 +93,8 @@ class YHAITextInputView: UIView {
textView.text = ""
textView.isScrollEnabled = false
self.endEditing(true)
textLabel.isHidden = true
textView.isHidden = false
}
@objc func didStopSendBtnClicked() {
......@@ -103,10 +105,12 @@ class YHAITextInputView: UIView {
let v = YHAutoTextView()
v.backgroundColor = .clear
v.font = .PFSC_R(ofSize: 14)
v.textColor = .mainTextColor
v.placeHolder = "有什么问题尽管问我"
v.textChange = {
[weak self] text in
guard let self = self else { return }
self.textLabel.text = text
if status != .loading {
status = text.count > 0 ? .enableSend : .disableSend
}
......@@ -114,6 +118,22 @@ class YHAITextInputView: UIView {
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) {
fatalError("init(coder:) has not been implemented")
}
......@@ -154,6 +174,7 @@ class YHAITextInputView: UIView {
contentView.addSubview(sendBtn)
contentView.addSubview(loadingImgView)
contentView.addSubview(textView)
contentView.addSubview(textLabel)
status = .disableSend
whiteView.snp.makeConstraints { make in
......@@ -189,6 +210,12 @@ class YHAITextInputView: UIView {
make.bottom.equalTo(-(11-YHAutoTextView.verticalGap))
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()
}
......@@ -203,7 +230,17 @@ class YHAITextInputView: UIView {
let isKeyboardShow = notification.name == UIResponder.keyboardWillShowNotification
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
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