// // KeyboardBaseView.swift // AiKeyboard // // Created by Destiny on 2025/4/25. // import UIKit protocol KeyboardBaseViewDelegate: NSObjectProtocol { func pasteBtnClickAction() func deleteBtnClickAction() func clearBtnClickAction() func sendBtnClickAction() // 长按删除方法 func deleteBtnLongPressBegin() func deleteBtnLongPressEnd() func jumpToSettings() func tipsCloseBtnAction() } class KeyboardBaseView: UIView { weak var delegate: KeyboardBaseViewDelegate? lazy var topView: UIView = { let view = UIView() view.isHidden = true view.backgroundColor = .clear return view }() lazy var allowView: UIView = { let view = UIView() view.backgroundColor = .hexStringColor(hexString: "#FFF8EF") view.layer.cornerRadius = 9 return view }() lazy var allowTitleLabel: UILabel = { let label = UILabel() label.text = "“允许”【从其他app粘贴】,不再弹出询问弹窗>" label.font = .systemFont(ofSize: 11) label.textColor = .hexStringColor(hexString: "#FFA616") label.isUserInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action: #selector(settingTitleClickAction)) label.addGestureRecognizer(tap) return label }() lazy var allowCloseBtn: UIButton = { let button = UIButton() button.setBackgroundImage(UIImage(named: "keyboard_paste_tip_close_icon"), for: .normal) button.addTarget(self, action: #selector(closeBtnClickAction), for: .touchUpInside) return button }() lazy var pasteBtn: UIButton = { let button = UIButton() button.setBackgroundImage(UIImage(named: "keyboard_paste_btn_bg"), for: .normal) button.setTitle("粘贴", for: .normal) button.setTitleColor(.white, for: .normal) button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium) button.addTarget(self, action: #selector(pasteBtnAction), for: .touchUpInside) return button }() lazy var deleteBtn: UIButton = { let button = UIButton() button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal) button.setTitle("删除", for: .normal) button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal) button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium) button.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside) // 添加长按手势 let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleDeleteLongPress(_:))) longPressGesture.minimumPressDuration = 0.5 // 设置长按触发时间为0.5秒 button.addGestureRecognizer(longPressGesture) return button }() lazy var clearBtn: UIButton = { let button = UIButton() button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal) button.setTitle("清空", for: .normal) button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal) button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium) button.addTarget(self, action: #selector(clearBtnAction), for: .touchUpInside) return button }() lazy var sendBtn: UIButton = { let button = UIButton() button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal) button.setTitle("发送", for: .normal) button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal) button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium) button.addTarget(self, action: #selector(sendBtnAction), for: .touchUpInside) return button }() lazy var dialogueView: UIView = { let view = UIView() view.layer.cornerRadius = 10 view.backgroundColor = .white return view }() // 对话框 lazy var dialogueTitleLabel: UILabel = { let label = UILabel() let attr = NSMutableAttributedString() let copyIcon = NSTextAttachment(image: UIImage(named: "keyboard_copy_icon")!) copyIcon.bounds = CGRect(x: -4, y: -4, width: 18, height: 18) let copyAttr = NSAttributedString(attachment: copyIcon) attr.append(copyAttr) let titleText = NSMutableAttributedString(string: "复制对方的话自动粘贴") titleText.addAttributes([.font: UIFont.systemFont(ofSize: 14, weight: .medium), .foregroundColor: UIColor.hexStringColor(hexString: "#996DFF")], range: NSRange(location: 0, length: titleText.length)) attr.append(titleText) label.attributedText = attr return label }() lazy var dialogueLabel: UILabel = { let label = UILabel() label.isHidden = true label.textAlignment = .center label.text = "" label.textColor = .hexStringColor(hexString: "#996DFF") label.font = .systemFont(ofSize: 14, weight: .medium) return label }() lazy var dialogueClearBtn: UIButton = { let btn = UIButton() btn.isHidden = true btn.setImage(UIImage(named: "keyboard_text_clear_btn"), for: .normal) btn.addTarget(self, action: #selector(dialogueClearBtnAction), for: .touchUpInside) return btn }() override init(frame: CGRect) { super.init(frame: frame) setUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension KeyboardBaseView { func setPasteStr(content: String) { self.dialogueTitleLabel.isHidden = true self.dialogueLabel.isHidden = false self.dialogueClearBtn.isHidden = false self.dialogueLabel.text = content } } extension KeyboardBaseView { @objc func settingTitleClickAction() { delegate?.jumpToSettings() } // 对话框清除按钮 @objc func dialogueClearBtnAction() { self.dialogueTitleLabel.isHidden = false self.dialogueLabel.isHidden = true self.dialogueClearBtn.isHidden = true self.dialogueLabel.text = "" } // 粘贴按钮 @objc func pasteBtnAction() { delegate?.pasteBtnClickAction() } // 删除按钮 @objc func deleteBtnAction() { delegate?.deleteBtnClickAction() } // 处理长按手势 @objc func handleDeleteLongPress(_ gesture: UILongPressGestureRecognizer) { switch gesture.state { case .began: // 长按开始 delegate?.deleteBtnLongPressBegin() case .ended, .cancelled: // 长按结束或取消 delegate?.deleteBtnLongPressEnd() default: break } } // 清除按钮 @objc func clearBtnAction() { delegate?.clearBtnClickAction() } // 发送按钮 @objc func sendBtnAction() { delegate?.sendBtnClickAction() } // 关闭提示框 @objc func closeBtnClickAction() { delegate?.tipsCloseBtnAction() } } extension KeyboardBaseView { func setUI() { self.addSubview(topView) topView.snp.makeConstraints { make in make.left.equalTo(12) make.right.equalTo(-12) make.top.equalTo(0) make.height.equalTo(0) } self.topView.addSubview(allowView) allowView.snp.makeConstraints { make in make.top.leading.right.equalTo(0) make.height.equalTo(24) } allowView.addSubview(allowTitleLabel) allowTitleLabel.snp.makeConstraints { make in make.left.equalTo(6) make.centerY.equalToSuperview() } allowView.addSubview(allowCloseBtn) allowCloseBtn.snp.makeConstraints { make in make.size.equalTo(CGSize(width: 10, height: 10)) make.right.equalTo(-8) make.centerY.equalToSuperview() } self.addSubview(pasteBtn) pasteBtn.snp.makeConstraints { make in make.size.equalTo(CGSize(width: 58, height: 46)) make.right.equalTo(-10) make.top.equalTo(topView.snp.bottom) } self.addSubview(deleteBtn) deleteBtn.snp.makeConstraints { make in make.size.equalTo(CGSize(width: 58, height: 46)) make.right.equalTo(-10) make.top.equalTo(pasteBtn.snp.bottom).offset(6) } self.addSubview(clearBtn) clearBtn.snp.makeConstraints { make in make.size.equalTo(CGSize(width: 58, height: 46)) make.right.equalTo(-10) make.top.equalTo(deleteBtn.snp.bottom).offset(6) } self.addSubview(sendBtn) sendBtn.snp.makeConstraints { make in make.size.equalTo(CGSize(width: 58, height: 52)) make.right.equalTo(-10) make.top.equalTo(clearBtn.snp.bottom).offset(6) } self.addSubview(dialogueView) dialogueView.snp.makeConstraints { make in make.height.equalTo(46) make.top.equalTo(topView.snp.bottom) make.left.equalTo(10) make.right.equalTo(pasteBtn.snp.left).offset(-6) } dialogueView.addSubview(dialogueTitleLabel) dialogueTitleLabel.snp.makeConstraints { make in make.center.equalToSuperview() } dialogueView.addSubview(dialogueClearBtn) dialogueClearBtn.snp.makeConstraints { make in make.size.equalTo(CGSize(width: 16, height: 16)) make.right.equalTo(-10) make.centerY.equalToSuperview() } dialogueView.addSubview(dialogueLabel) dialogueLabel.snp.makeConstraints { make in make.left.equalTo(12) make.right.equalTo(dialogueClearBtn.snp.left).offset(-12) make.centerY.equalToSuperview() } } }