KeyboardBaseView.swift 10 KB

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