123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // TSChatInputBarVC.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/2/14.
- //
- import UIKit
- class TSChatInputBarVC: TSBaseVC, UITextViewDelegate {
-
- var sendComplete:(([Any])->Void)?
- //AI是否正在回答问题
- var isAIAnswering:Bool = false{
- didSet{
- sendEnabled(enabled: !isAIAnswering)
- setBtnHidden()
- }
- }
-
- lazy var InputBarView:UIView = {
- let InputBarView = UIView()
- InputBarView.backgroundColor = "#222222".uiColor
- InputBarView.cornerRadius = 28.0
- return InputBarView
- }()
-
- lazy var sendBtn: UIButton = {
- let sendBtn = UIButton.createButton(image: UIImage(named: "chat_send")) { [weak self] in
- guard let self = self else { return }
- sendMsg()
- }
- sendBtn.isEnabled = false
- return sendBtn
- }()
-
- lazy var stopBtn: UIButton = {
- let stopBtn = UIButton.createButton(image: UIImage(named: "chat_stop")) { [weak self] in
- guard let self = self else { return }
- NotificationCenter.default.post(name: .kAIStopRespondNotification, object: nil, userInfo: nil)
- }
- stopBtn.isHidden = true
- return stopBtn
- }()
-
- lazy var magnifyBtn: UIButton = {
- let magnifyBtn = UIButton.createButton(image: UIImage(named: "chat_send_magnify")) { [weak self] in
- guard let self = self else { return }
- let vc = TSChatInputFullScreenVC()
-
- vc.text = textView.text
- vc.sendComplete = { [weak self] date in
- guard let self = self else { return }
- if let text = date.first as? String {
- textView.text = text
- textView.resignFirstResponder()
- textDidChange()
- scrollTextViewToBottom()
- sendComplete?(date)
- }
- }
-
- vc.closeComplete = { [weak self] text in
- guard let self = self else { return }
- textView.text = text
- textView.resignFirstResponder()
- textDidChange()
- scrollTextViewToBottom()
- }
-
- kPresentModalVC(target: self, modelVC: vc)
-
- }
- magnifyBtn.isHidden = true
- return magnifyBtn
- }()
-
- private let minHeight: CGFloat = 56//24
- private let maxHeight: CGFloat = 154
- lazy var textView: TSPlaceholderTextView = {
- let textView = TSPlaceholderTextView()
- textView.backgroundColor = .clear
- textView.textColor = .white
- textView.delegate = self
- textView.font = .font(size: 16)
- textView.clipsToBounds = true
- textView.isScrollEnabled = false
- textView.tintColor = .themeColor
- textView.returnKeyType = .send
- textView.placeholder = "Send a message".localized
- textView.placeholderColor = .white.withAlphaComponent(0.4)
- textView.placeholderLabel.font = .font(size: 16.0)
- textView.textInsets = UIEdgeInsets(top: 18, left: 16, bottom: 14, right: 0)
- return textView
- }()
-
- override func createView() {
- setNavBarViewHidden(true)
-
- view.backgroundColor = .clear
-
- contentView.addSubview(InputBarView)
- InputBarView.addSubview(textView)
- InputBarView.addSubview(magnifyBtn)
- InputBarView.addSubview(stopBtn)
- InputBarView.addSubview(sendBtn)
-
- InputBarView.snp.makeConstraints { make in
- make.leading.equalTo(16)
- make.trailing.equalTo(-16)
- make.top.equalTo(12)
- make.bottom.equalTo(-12)
- }
-
- textView.snp.makeConstraints { make in
- make.leading.equalTo(0)
- make.trailing.equalTo(-60)
- make.top.equalTo(0)
- make.bottom.equalTo(0)
- make.height.equalTo(minHeight)
- }
-
- stopBtn.snp.makeConstraints { make in
- make.trailing.equalTo(-16)
- make.bottom.equalTo(-16)
- make.width.equalTo(24)
- make.height.equalTo(24)
- }
-
- sendBtn.snp.makeConstraints { make in
- make.trailing.equalTo(-16)
- make.bottom.equalTo(-16)
- make.width.equalTo(24)
- make.height.equalTo(24)
- }
-
- magnifyBtn.snp.makeConstraints { make in
- make.trailing.equalTo(-16)
- make.top.equalTo(16)
- make.width.equalTo(24)
- make.height.equalTo(24)
- }
- }
-
-
- override func dealThings() {
- // 监听文本变化事件
- NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: UITextView.textDidChangeNotification, object: textView)
- NotificationCenter.default.addObserver(self, selector: #selector(handleAIAnsweringNotification(_:)), name: .kAIAnsweringNotification, object: nil)
- }
- func sendMsg(){
-
- if sendBtn.isEnabled == false {
- return
- }
-
- // if textView.text.replacingOccurrences(of: " ", with: "").count <= 0 {
- // return
- // }
-
- if let string = textView.text {
- sendComplete?([string])
- textView.resignFirstResponder()
- }
- }
-
- func sendEnabled(enabled:Bool){
- if enabled == true,
- isAIAnswering == false,
- textView.text.replacingOccurrences(of: " ", with: "").count > 0 {
- sendBtn.isEnabled = true
- }else{
- sendBtn.isEnabled = false
- }
- }
-
- func emptyInput() {
- textView.text = ""
- textDidChange()
- magnifyBtn.isHidden = true
- }
-
-
- @objc func handleAIAnsweringNotification(_ notification: Notification) {
- if let userInfo = notification.userInfo, let boolValue = userInfo[kIsAIAnswering] as? Bool {
- isAIAnswering = boolValue
- }
- }
-
- func setBtnHidden() {
- if isAIAnswering {
- sendBtn.isHidden = true
- stopBtn.isHidden = false
- }else{
- sendBtn.isHidden = false
- stopBtn.isHidden = true
- }
- }
-
-
-
-
- deinit {
- // 移除通知监听
- NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: textView)
- }
- }
- extension TSChatInputBarVC {
- @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
- textView.isScrollEnabled = false
- magnifyBtn.isHidden = true
- } else if newHeight > maxHeight {
- newHeight = maxHeight
- textView.isScrollEnabled = true
- magnifyBtn.isHidden = false
- } else {
- textView.isScrollEnabled = false
- magnifyBtn.isHidden = true
- }
-
-
- sendEnabled(enabled: textView.text.count > 0)
- // 更新输入框的高度
- textView.snp.updateConstraints { make in
- make.height.equalTo(newHeight)
- }
- }
- private func scrollTextViewToBottom() {
- let bottomOffset = CGPoint(x: 0, y: textView.contentSize.height - textView.bounds.size.height)
- textView.setContentOffset(bottomOffset, animated: true)
- }
-
- }
- extension TSChatInputBarVC {
-
- // 实现 UITextViewDelegate 协议方法,控制 return 键行为
- func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
- if text == "\n" {
- // 当输入为换行符(即按下 return 键)时,执行相应操作
- sendMsg()
- return false
- }
- return true
- }
- }
|