KeyboardBaseView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 topView: UIView = {
  20. let view = UIView()
  21. view.isHidden = true
  22. view.backgroundColor = .clear
  23. return view
  24. }()
  25. lazy var allowView: UIView = {
  26. let view = UIView()
  27. view.backgroundColor = .hexStringColor(hexString: "#FFF8EF")
  28. view.layer.cornerRadius = 9
  29. return view
  30. }()
  31. lazy var allowTitleLabel: UILabel = {
  32. let label = UILabel()
  33. label.text = "“允许”【从其他app粘贴】,不再弹出询问弹窗>"
  34. label.font = .systemFont(ofSize: 11)
  35. label.textColor = .hexStringColor(hexString: "#FFA616")
  36. return label
  37. }()
  38. lazy var allowCloseBtn: UIButton = {
  39. let button = UIButton()
  40. button.setBackgroundImage(UIImage(named: "keyboard_paste_tip_close_icon"), for: .normal)
  41. button.addTarget(self, action: #selector(closeBtnClickAction), for: .touchUpInside)
  42. return button
  43. }()
  44. lazy var pasteBtn: UIButton = {
  45. let button = UIButton()
  46. button.setBackgroundImage(UIImage(named: "keyboard_paste_btn_bg"), for: .normal)
  47. button.setTitle("粘贴", for: .normal)
  48. button.setTitleColor(.white, for: .normal)
  49. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  50. button.addTarget(self, action: #selector(pasteBtnAction), for: .touchUpInside)
  51. return button
  52. }()
  53. lazy var deleteBtn: UIButton = {
  54. let button = UIButton()
  55. button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal)
  56. button.setTitle("删除", for: .normal)
  57. button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal)
  58. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  59. button.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
  60. // 添加长按手势
  61. let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleDeleteLongPress(_:)))
  62. longPressGesture.minimumPressDuration = 0.5 // 设置长按触发时间为0.5秒
  63. button.addGestureRecognizer(longPressGesture)
  64. return button
  65. }()
  66. lazy var clearBtn: UIButton = {
  67. let button = UIButton()
  68. button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal)
  69. button.setTitle("清空", for: .normal)
  70. button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal)
  71. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  72. button.addTarget(self, action: #selector(clearBtnAction), for: .touchUpInside)
  73. return button
  74. }()
  75. lazy var sendBtn: UIButton = {
  76. let button = UIButton()
  77. button.setBackgroundImage(UIImage(named: "keyboard_normal_btn_bg"), for: .normal)
  78. button.setTitle("发送", for: .normal)
  79. button.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.9), for: .normal)
  80. button.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
  81. button.addTarget(self, action: #selector(sendBtnAction), for: .touchUpInside)
  82. return button
  83. }()
  84. lazy var dialogueView: UIView = {
  85. let view = UIView()
  86. view.layer.cornerRadius = 10
  87. view.backgroundColor = .white
  88. return view
  89. }()
  90. // 对话框
  91. lazy var dialogueTitleLabel: UILabel = {
  92. let label = UILabel()
  93. let attr = NSMutableAttributedString()
  94. let copyIcon = NSTextAttachment(image: UIImage(named: "keyboard_copy_icon")!)
  95. copyIcon.bounds = CGRect(x: -4, y: -4, width: 18, height: 18)
  96. let copyAttr = NSAttributedString(attachment: copyIcon)
  97. attr.append(copyAttr)
  98. let titleText = NSMutableAttributedString(string: "复制对方的话自动粘贴")
  99. titleText.addAttributes([.font: UIFont.systemFont(ofSize: 14, weight: .medium), .foregroundColor: UIColor.hexStringColor(hexString: "#996DFF")], range: NSRange(location: 0, length: titleText.length))
  100. attr.append(titleText)
  101. label.attributedText = attr
  102. return label
  103. }()
  104. lazy var dialogueLabel: UILabel = {
  105. let label = UILabel()
  106. label.isHidden = true
  107. label.textAlignment = .center
  108. label.text = ""
  109. label.textColor = .hexStringColor(hexString: "#996DFF")
  110. label.font = .systemFont(ofSize: 14, weight: .medium)
  111. return label
  112. }()
  113. lazy var dialogueClearBtn: UIButton = {
  114. let btn = UIButton()
  115. btn.isHidden = true
  116. btn.setImage(UIImage(named: "keyboard_text_clear_btn"), for: .normal)
  117. btn.addTarget(self, action: #selector(dialogueClearBtnAction), for: .touchUpInside)
  118. return btn
  119. }()
  120. override init(frame: CGRect) {
  121. super.init(frame: frame)
  122. setUI()
  123. }
  124. required init?(coder: NSCoder) {
  125. fatalError("init(coder:) has not been implemented")
  126. }
  127. }
  128. extension KeyboardBaseView {
  129. func setPasteStr(content: String) {
  130. self.dialogueTitleLabel.isHidden = true
  131. self.dialogueLabel.isHidden = false
  132. self.dialogueClearBtn.isHidden = false
  133. self.dialogueLabel.text = content
  134. }
  135. }
  136. extension KeyboardBaseView {
  137. // 对话框清除按钮
  138. @objc func dialogueClearBtnAction() {
  139. self.dialogueTitleLabel.isHidden = false
  140. self.dialogueLabel.isHidden = true
  141. self.dialogueClearBtn.isHidden = true
  142. self.dialogueLabel.text = ""
  143. }
  144. // 粘贴按钮
  145. @objc func pasteBtnAction() {
  146. delegate?.pasteBtnClickAction()
  147. }
  148. // 删除按钮
  149. @objc func deleteBtnAction() {
  150. delegate?.deleteBtnClickAction()
  151. }
  152. // 处理长按手势
  153. @objc func handleDeleteLongPress(_ gesture: UILongPressGestureRecognizer) {
  154. switch gesture.state {
  155. case .began:
  156. // 长按开始
  157. delegate?.deleteBtnLongPressBegin()
  158. case .ended, .cancelled:
  159. // 长按结束或取消
  160. delegate?.deleteBtnLongPressEnd()
  161. default:
  162. break
  163. }
  164. }
  165. // 清除按钮
  166. @objc func clearBtnAction() {
  167. delegate?.clearBtnClickAction()
  168. }
  169. // 发送按钮
  170. @objc func sendBtnAction() {
  171. delegate?.sendBtnClickAction()
  172. }
  173. // 关闭提示框
  174. @objc func closeBtnClickAction() {
  175. self.topView.snp.updateConstraints { make in
  176. make.height.equalTo(0)
  177. }
  178. self.topView.isHidden = true
  179. }
  180. }
  181. extension KeyboardBaseView {
  182. func setUI() {
  183. self.addSubview(topView)
  184. topView.snp.makeConstraints { make in
  185. make.left.equalTo(12)
  186. make.right.equalTo(-12)
  187. make.top.equalTo(0)
  188. make.height.equalTo(0)
  189. }
  190. self.topView.addSubview(allowView)
  191. allowView.snp.makeConstraints { make in
  192. make.top.leading.right.equalTo(0)
  193. make.height.equalTo(24)
  194. }
  195. allowView.addSubview(allowTitleLabel)
  196. allowTitleLabel.snp.makeConstraints { make in
  197. make.left.equalTo(6)
  198. make.centerY.equalToSuperview()
  199. }
  200. allowView.addSubview(allowCloseBtn)
  201. allowCloseBtn.snp.makeConstraints { make in
  202. make.size.equalTo(CGSize(width: 10, height: 10))
  203. make.right.equalTo(-8)
  204. make.centerY.equalToSuperview()
  205. }
  206. self.addSubview(pasteBtn)
  207. pasteBtn.snp.makeConstraints { make in
  208. make.size.equalTo(CGSize(width: 58, height: 46))
  209. make.right.equalTo(-10)
  210. make.top.equalTo(topView.snp.bottom)
  211. }
  212. self.addSubview(deleteBtn)
  213. deleteBtn.snp.makeConstraints { make in
  214. make.size.equalTo(CGSize(width: 58, height: 46))
  215. make.right.equalTo(-10)
  216. make.top.equalTo(pasteBtn.snp.bottom).offset(6)
  217. }
  218. self.addSubview(clearBtn)
  219. clearBtn.snp.makeConstraints { make in
  220. make.size.equalTo(CGSize(width: 58, height: 46))
  221. make.right.equalTo(-10)
  222. make.top.equalTo(deleteBtn.snp.bottom).offset(6)
  223. }
  224. self.addSubview(sendBtn)
  225. sendBtn.snp.makeConstraints { make in
  226. make.size.equalTo(CGSize(width: 58, height: 52))
  227. make.right.equalTo(-10)
  228. make.top.equalTo(clearBtn.snp.bottom).offset(6)
  229. }
  230. self.addSubview(dialogueView)
  231. dialogueView.snp.makeConstraints { make in
  232. make.height.equalTo(46)
  233. make.top.equalTo(topView.snp.bottom)
  234. make.left.equalTo(10)
  235. make.right.equalTo(pasteBtn.snp.left).offset(-6)
  236. }
  237. dialogueView.addSubview(dialogueTitleLabel)
  238. dialogueTitleLabel.snp.makeConstraints { make in
  239. make.center.equalToSuperview()
  240. }
  241. dialogueView.addSubview(dialogueClearBtn)
  242. dialogueClearBtn.snp.makeConstraints { make in
  243. make.size.equalTo(CGSize(width: 16, height: 16))
  244. make.right.equalTo(-10)
  245. make.centerY.equalToSuperview()
  246. }
  247. dialogueView.addSubview(dialogueLabel)
  248. dialogueLabel.snp.makeConstraints { make in
  249. make.left.equalTo(12)
  250. make.right.equalTo(dialogueClearBtn.snp.left).offset(-12)
  251. make.centerY.equalToSuperview()
  252. }
  253. }
  254. }