KeyboardBaseView.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // KeyboardBaseView.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/25.
  6. //
  7. import UIKit
  8. protocol KeyboardBaseViewDelegate: NSObjectProtocol {
  9. func pasteBtnClickAction()
  10. func deleteBtnClickAction()
  11. func clearBtnClickAction()
  12. func sendBtnClickAction()
  13. // 长按删除方法
  14. func deleteBtnLongPressBegin()
  15. func deleteBtnLongPressEnd()
  16. }
  17. class KeyboardBaseView: UIView {
  18. weak var delegate: KeyboardBaseViewDelegate?
  19. lazy var pasteBtn: UIButton = {
  20. let button = UIButton()
  21. button.setBackgroundImage(UIImage(named: "keyboard_paste_btn_bg"), for: .normal)
  22. button.setTitle("粘贴", for: .normal)
  23. button.setTitleColor(.white, for: .normal)
  24. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  25. button.addTarget(self, action: #selector(pasteBtnAction), for: .touchUpInside)
  26. return button
  27. }()
  28. lazy var deleteBtn: UIButton = {
  29. let button = UIButton()
  30. button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal)
  31. button.setTitle("删除", for: .normal)
  32. button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal)
  33. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  34. button.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
  35. // 添加长按手势
  36. let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleDeleteLongPress(_:)))
  37. longPressGesture.minimumPressDuration = 0.5 // 设置长按触发时间为0.5秒
  38. button.addGestureRecognizer(longPressGesture)
  39. return button
  40. }()
  41. lazy var clearBtn: UIButton = {
  42. let button = UIButton()
  43. button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal)
  44. button.setTitle("清空", for: .normal)
  45. button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal)
  46. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  47. button.addTarget(self, action: #selector(clearBtnAction), for: .touchUpInside)
  48. return button
  49. }()
  50. lazy var sendBtn: UIButton = {
  51. let button = UIButton()
  52. button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal)
  53. button.setTitle("发送", for: .normal)
  54. button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal)
  55. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  56. button.addTarget(self, action: #selector(sendBtnAction), for: .touchUpInside)
  57. return button
  58. }()
  59. lazy var dialogueView: UIView = {
  60. let view = UIView()
  61. view.layer.cornerRadius = 10
  62. view.backgroundColor = .white
  63. return view
  64. }()
  65. // 对话框
  66. lazy var dialogueTitleLabel: UILabel = {
  67. let label = UILabel()
  68. let attr = NSMutableAttributedString()
  69. let copyIcon = NSTextAttachment(image: UIImage(named: "keyboard_copy_icon")!)
  70. copyIcon.bounds = CGRect(x: -4, y: -4, width: 18, height: 18)
  71. let copyAttr = NSAttributedString(attachment: copyIcon)
  72. attr.append(copyAttr)
  73. let titleText = NSMutableAttributedString(string: "复制对方的话自动粘贴")
  74. titleText.addAttributes([.font: UIFont.systemFont(ofSize: 14, weight: .medium), .foregroundColor: UIColor.hexStringColor(hexString: "#996DFF")], range: NSRange(location: 0, length: titleText.length))
  75. attr.append(titleText)
  76. label.attributedText = attr
  77. return label
  78. }()
  79. lazy var dialogueLabel: UILabel = {
  80. let label = UILabel()
  81. label.isHidden = true
  82. label.textAlignment = .center
  83. label.text = ""
  84. label.textColor = .hexStringColor(hexString: "#996DFF")
  85. label.font = .systemFont(ofSize: 14, weight: .medium)
  86. return label
  87. }()
  88. lazy var dialogueClearBtn: UIButton = {
  89. let btn = UIButton()
  90. btn.isHidden = true
  91. btn.setImage(UIImage(named: "keyboard_text_clear_btn"), for: .normal)
  92. btn.addTarget(self, action: #selector(dialogueClearBtnAction), for: .touchUpInside)
  93. return btn
  94. }()
  95. override init(frame: CGRect) {
  96. super.init(frame: frame)
  97. setUI()
  98. }
  99. required init?(coder: NSCoder) {
  100. fatalError("init(coder:) has not been implemented")
  101. }
  102. }
  103. extension KeyboardBaseView {
  104. func setPasteStr(content: String) {
  105. self.dialogueTitleLabel.isHidden = true
  106. self.dialogueLabel.isHidden = false
  107. self.dialogueClearBtn.isHidden = false
  108. self.dialogueLabel.text = content
  109. }
  110. }
  111. extension KeyboardBaseView {
  112. // 对话框清除按钮
  113. @objc func dialogueClearBtnAction() {
  114. self.dialogueTitleLabel.isHidden = false
  115. self.dialogueLabel.isHidden = true
  116. self.dialogueClearBtn.isHidden = true
  117. self.dialogueLabel.text = ""
  118. }
  119. // 粘贴按钮
  120. @objc func pasteBtnAction() {
  121. delegate?.pasteBtnClickAction()
  122. }
  123. // 删除按钮
  124. @objc func deleteBtnAction() {
  125. delegate?.deleteBtnClickAction()
  126. }
  127. // 处理长按手势
  128. @objc func handleDeleteLongPress(_ gesture: UILongPressGestureRecognizer) {
  129. switch gesture.state {
  130. case .began:
  131. // 长按开始
  132. delegate?.deleteBtnLongPressBegin()
  133. case .ended, .cancelled:
  134. // 长按结束或取消
  135. delegate?.deleteBtnLongPressEnd()
  136. default:
  137. break
  138. }
  139. }
  140. // 清除按钮
  141. @objc func clearBtnAction() {
  142. delegate?.clearBtnClickAction()
  143. }
  144. // 发送按钮
  145. @objc func sendBtnAction() {
  146. delegate?.sendBtnClickAction()
  147. }
  148. }
  149. extension KeyboardBaseView {
  150. func setUI() {
  151. self.addSubview(pasteBtn)
  152. pasteBtn.snp.makeConstraints { make in
  153. make.size.equalTo(CGSize(width: 58, height: 46))
  154. make.right.equalTo(-10)
  155. make.top.equalTo(0)
  156. }
  157. self.addSubview(deleteBtn)
  158. deleteBtn.snp.makeConstraints { make in
  159. make.size.equalTo(CGSize(width: 58, height: 46))
  160. make.right.equalTo(-10)
  161. make.top.equalTo(pasteBtn.snp.bottom).offset(6)
  162. }
  163. self.addSubview(clearBtn)
  164. clearBtn.snp.makeConstraints { make in
  165. make.size.equalTo(CGSize(width: 58, height: 46))
  166. make.right.equalTo(-10)
  167. make.top.equalTo(deleteBtn.snp.bottom).offset(6)
  168. }
  169. self.addSubview(sendBtn)
  170. sendBtn.snp.makeConstraints { make in
  171. make.size.equalTo(CGSize(width: 58, height: 52))
  172. make.right.equalTo(-10)
  173. make.top.equalTo(clearBtn.snp.bottom).offset(6)
  174. }
  175. self.addSubview(dialogueView)
  176. dialogueView.snp.makeConstraints { make in
  177. make.height.equalTo(46)
  178. make.top.equalTo(0)
  179. make.left.equalTo(10)
  180. make.right.equalTo(pasteBtn.snp.left).offset(-6)
  181. }
  182. dialogueView.addSubview(dialogueTitleLabel)
  183. dialogueTitleLabel.snp.makeConstraints { make in
  184. make.center.equalToSuperview()
  185. }
  186. dialogueView.addSubview(dialogueClearBtn)
  187. dialogueClearBtn.snp.makeConstraints { make in
  188. make.size.equalTo(CGSize(width: 16, height: 16))
  189. make.right.equalTo(-10)
  190. make.centerY.equalToSuperview()
  191. }
  192. dialogueView.addSubview(dialogueLabel)
  193. dialogueLabel.snp.makeConstraints { make in
  194. make.left.equalTo(12)
  195. make.right.equalTo(dialogueClearBtn.snp.left).offset(-12)
  196. make.centerY.equalToSuperview()
  197. }
  198. }
  199. }