BFSubtitleSettingView.swift 12 KB

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