BFSubtitleSettingView.swift 13 KB

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