BFSubtitleSettingView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // BFSubtitleSettingView.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by ak on 2021/12/7.
  6. // 功能:设置字幕操作面板
  7. import BFCommonKit
  8. import BFMediaKit
  9. import BFUIKit
  10. import Foundation
  11. typealias SubtitleSettingCallBack = (_ subtitileModel: PQEditSubTitleModel) -> Void
  12. class BFSubtitleSettingView: UIView {
  13. required init?(coder _: NSCoder) {
  14. fatalError("init(coder:) has not been implemented")
  15. }
  16. // 最后一次选择的样式 BTN
  17. var lastSelectStyleBtn: UIButton = UIButton()
  18. // 最后一次选择的位置 BTN
  19. var lastSelectPointBtn: UIButton = UIButton()
  20. // 最后一次选择的字号的 BTN
  21. var lastSelectwordSizeBtn: UIButton = UIButton()
  22. var subtitleSettingCallBack: SubtitleSettingCallBack?
  23. var subtitle: PQEditSubTitleModel = PQEditSubTitleModel()
  24. // 样式配置
  25. var styleConfig: [Int: [String: Any]] = Dictionary()
  26. override init(frame: CGRect) {
  27. super.init(frame: frame)
  28. backgroundColor = UIColor.clear
  29. addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hidden)))
  30. let backView = UIView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  31. backView.backgroundColor = .black
  32. addSubview(backView)
  33. backView.snp.makeConstraints { make in
  34. make.right.equalTo(self.snp.right)
  35. make.bottom.equalTo(self.snp.bottom)
  36. make.width.equalTo(cScreenWidth)
  37. make.height.equalTo(220)
  38. }
  39. styleConfig = [0: ["fontColor": UIColor.hexColor(hexadecimal: "#FFCF53"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  40. 1: ["fontColor": UIColor.hexColor(hexadecimal: "#FF9292"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  41. 2: ["fontColor": UIColor.hexColor(hexadecimal: "#80C2FF"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  42. 3: ["fontColor": UIColor.hexColor(hexadecimal: "#80E4AB"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  43. 4: ["fontColor": UIColor.hexColor(hexadecimal: "#FFFFFF"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  44. 5: ["fontColor": UIColor.hexColor(hexadecimal: "#000000"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.white, "backgroundAlpha": 0.0],
  45. 6: ["fontColor": UIColor.hexColor(hexadecimal: "#FFFFFF"), "backgroundColor": UIColor.black, "strokeColor": UIColor.black, "backgroundAlpha": 0.6]]
  46. // 字体样式
  47. for i in 0...6 {
  48. let btn = UIButton(type: .custom)
  49. btn.setImage(imageInRecordScreenKit(by: "wordStyle\(i + 1)"), for: .normal)
  50. btn.setBackgroundImage(UIImage(color: .black), for: .normal)
  51. btn.frame = CGRect(x: 18 + i * (40 + 10), y: 22, width: 40, height: 40)
  52. btn.tag = i
  53. if i == 0 {
  54. btn.isSelected = true
  55. btn.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
  56. btn.layer.borderWidth = 1.5
  57. lastSelectStyleBtn = btn
  58. styleSetting(sender: lastSelectStyleBtn)
  59. }
  60. btn.addTarget(self, action: #selector(styleSetting(sender:)), for: .touchUpInside)
  61. backView.addSubview(btn)
  62. btn.addCorner(corner: 20.0)
  63. }
  64. // 位置
  65. let pointTitle = UILabel()
  66. pointTitle.textAlignment = .center
  67. pointTitle.font = UIFont.systemFont(ofSize: 15)
  68. pointTitle.textColor = .white
  69. pointTitle.text = "位置"
  70. pointTitle.alpha = 0.6
  71. backView.addSubview(pointTitle)
  72. pointTitle.snp.makeConstraints { make in
  73. make.width.equalTo(40)
  74. make.height.equalTo(25)
  75. make.left.equalToSuperview().offset(16)
  76. make.top.equalToSuperview().offset(89)
  77. }
  78. for i in 0...2 {
  79. let pointBtn = UIButton(type: .custom)
  80. pointBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#28BE67"), for: .selected)
  81. pointBtn.setTitleColor(.white, for: .normal)
  82. pointBtn.addTarget(self, action: #selector(pointSetting(sender:)), for: .touchUpInside)
  83. pointBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#1A1A1A")
  84. if i == 0 {
  85. pointBtn.isSelected = true
  86. pointBtn.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
  87. pointBtn.layer.borderWidth = 1.5
  88. lastSelectPointBtn = pointBtn
  89. pointBtn.setTitle("下", for: .normal)
  90. } else if i == 1 {
  91. pointBtn.setTitle("中", for: .normal)
  92. } else {
  93. pointBtn.setTitle("上", for: .normal)
  94. }
  95. pointBtn.addCorner(corner: 10)
  96. pointBtn.tag = i
  97. backView.addSubview(pointBtn)
  98. pointBtn.snp.makeConstraints { make in
  99. make.width.equalTo(40)
  100. make.height.equalTo(30)
  101. make.centerX.equalTo(pointTitle.snp.centerX).offset(60 + i * Int(40 + 10))
  102. make.centerY.equalTo(pointTitle.snp.centerY)
  103. }
  104. }
  105. // 字号
  106. let wordSizeTitle = UILabel()
  107. wordSizeTitle.textAlignment = .center
  108. wordSizeTitle.font = UIFont.systemFont(ofSize: 15)
  109. wordSizeTitle.textColor = .white
  110. wordSizeTitle.text = "字号"
  111. wordSizeTitle.alpha = 0.6
  112. backView.addSubview(wordSizeTitle)
  113. wordSizeTitle.snp.makeConstraints { make in
  114. make.width.equalTo(40)
  115. make.height.equalTo(25)
  116. make.right.equalToSuperview().offset(-100)
  117. make.top.equalTo(pointTitle.snp.top)
  118. }
  119. for i in 0...1 {
  120. let wordSizeBtn = UIButton(type: .custom)
  121. wordSizeBtn.addTarget(self, action: #selector(wordSizeSetting(sender:)), for: .touchUpInside)
  122. wordSizeBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#1A1A1A")
  123. wordSizeBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#28BE67"), for: .selected)
  124. wordSizeBtn.setTitleColor(.white, for: .normal)
  125. wordSizeBtn.setTitle(i == 0 ? "-" : "+", for: .normal)
  126. backView.addSubview(wordSizeBtn)
  127. wordSizeBtn.tag = i
  128. if i == 0 {
  129. wordSizeBtn.isSelected = true
  130. wordSizeBtn.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
  131. wordSizeBtn.layer.borderWidth = 1.5
  132. lastSelectwordSizeBtn = wordSizeBtn
  133. }
  134. wordSizeBtn.addCorner(corner: 10)
  135. wordSizeBtn.snp.makeConstraints { make in
  136. make.width.equalTo(30)
  137. make.height.equalTo(30)
  138. make.left.equalTo(wordSizeTitle.snp.right).offset(10 + i * Int(30 + 10))
  139. make.centerY.equalTo(pointTitle.snp.centerY)
  140. }
  141. }
  142. // 就是一个线
  143. let line = UIView()
  144. line.backgroundColor = UIColor.hexColor(hexadecimal: "#121212")
  145. backView.addSubview(line)
  146. line.snp.makeConstraints { make in
  147. make.width.equalTo(340)
  148. make.height.equalTo(2)
  149. make.left.equalToSuperview().offset(18)
  150. make.top.equalTo(wordSizeTitle.snp.bottom).offset(23)
  151. }
  152. // 不显示字幕
  153. let disabelTitle = UILabel()
  154. disabelTitle.textAlignment = .left
  155. disabelTitle.font = UIFont.boldSystemFont(ofSize: 17)
  156. disabelTitle.textColor = .white
  157. disabelTitle.text = "不显示字幕"
  158. backView.addSubview(disabelTitle)
  159. disabelTitle.snp.makeConstraints { make in
  160. make.width.equalTo(100)
  161. make.height.equalTo(24)
  162. make.left.equalToSuperview().offset(18)
  163. make.top.equalTo(line.snp.top).offset(17)
  164. }
  165. // 创建switch
  166. let disabeSwitch = UISwitch()
  167. disabeSwitch.isOn = false
  168. backView.addSubview(disabeSwitch)
  169. disabeSwitch.addTarget(self, action: #selector(switchChange(uiswitch:)), for: .valueChanged)
  170. disabeSwitch.snp.makeConstraints { make in
  171. make.width.equalTo(51)
  172. make.height.equalTo(31)
  173. make.right.equalToSuperview().offset(-18)
  174. make.top.equalTo(line.snp.top).offset(13)
  175. }
  176. // addCorner(roundingCorners: .topLeft, corner: 10)
  177. // addCorner(roundingCorners: .topRight, corner: 10)
  178. BFLog(message: "")
  179. }
  180. @objc func hidden() {
  181. isHidden = true
  182. }
  183. @objc func styleSetting(sender: UIButton) {
  184. lastSelectStyleBtn.isSelected = false
  185. lastSelectStyleBtn.layer.borderColor = UIColor.clear.cgColor
  186. sender.isSelected = true
  187. lastSelectStyleBtn = sender
  188. sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
  189. sender.layer.borderWidth = 1.5
  190. subtitle.setting.subtitleStyle = sender.tag
  191. let config = styleConfig[sender.tag]
  192. subtitle.setting.fontColor = config?["fontColor"] as! UIColor
  193. subtitle.setting.backgroundColor = config?["backgroundColor"] as! UIColor
  194. subtitle.setting.backgroundAlpha = Float(config?["backgroundAlpha"] as! Double)
  195. subtitle.setting.strokeColor = config?["strokeColor"] as! UIColor
  196. if subtitleSettingCallBack != nil {
  197. subtitleSettingCallBack!(subtitle)
  198. }
  199. }
  200. @objc func pointSetting(sender: UIButton) {
  201. lastSelectPointBtn.isSelected = false
  202. lastSelectPointBtn.layer.borderColor = UIColor.clear.cgColor
  203. sender.isSelected = true
  204. lastSelectPointBtn = sender
  205. sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
  206. sender.layer.borderWidth = 1.5
  207. subtitle.setting.subtitlePoint = sender.tag
  208. if subtitleSettingCallBack != nil {
  209. subtitleSettingCallBack!(subtitle)
  210. }
  211. }
  212. @objc func wordSizeSetting(sender: UIButton) {
  213. lastSelectwordSizeBtn.isSelected = false
  214. lastSelectwordSizeBtn.layer.borderColor = UIColor.clear.cgColor
  215. sender.isSelected = true
  216. lastSelectwordSizeBtn = sender
  217. sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
  218. sender.layer.borderWidth = 1.5
  219. if sender.tag == 0, subtitle.setting.subtitleSize > 20 {
  220. subtitle.setting.subtitleSize -= 10
  221. } else if sender.tag == 1, subtitle.setting.subtitleSize < 120 {
  222. subtitle.setting.subtitleSize += 10
  223. } else {
  224. BFLog(message: "设置字号超出范围!")
  225. }
  226. if subtitleSettingCallBack != nil {
  227. subtitleSettingCallBack!(subtitle)
  228. }
  229. }
  230. @objc func switchChange(uiswitch: UISwitch) {
  231. print("字幕开关状态\(uiswitch.isOn)")
  232. subtitle.setting.subtitleIsShow = !uiswitch.isOn
  233. if subtitleSettingCallBack != nil {
  234. subtitleSettingCallBack!(subtitle)
  235. }
  236. }
  237. }