BFSubtitleEditView.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // BFSubtitleEditView.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by ak on 2021/12/8.
  6. //
  7. import BFCommonKit
  8. import BFMediaKit
  9. import BFUIKit
  10. import Foundation
  11. typealias EditSubtitleDone = (_ text: String,_ index:Int) -> Void
  12. //字幕最大数限制
  13. //public let subtitleMaxlength:Int = 0
  14. class BFSubtitleEditView: UIView {
  15. var showSubtitleIndex = 0
  16. var editSubtitleDone: EditSubtitleDone?
  17. // var settingModel:BFSubtitileSettingModel = BFSubtitileSettingModel.init()
  18. /// 输入框
  19. lazy var textView: BFTextView = {
  20. let textView = BFTextView()
  21. textView.maxTextLengthRemind = ""
  22. textView.backgroundColor = UIColor.clear
  23. textView.textColor = UIColor.hexColor(hexadecimal: "#FFFFFF")
  24. textView.tintColor = UIColor.hexColor(hexadecimal: "#389AFF")
  25. textView.font = UIFont.systemFont(ofSize: 17)
  26. textView.placeHolder = ""
  27. textView.placeHolderColor = UIColor.hexColor(hexadecimal: "#555555")
  28. textView.delegate = self
  29. textView.textDidChangedHandle = { [weak self] _ in
  30. }
  31. if #available(iOS 11.0, *) {
  32. textView.contentInsetAdjustmentBehavior = .never
  33. }
  34. return textView
  35. }()
  36. required init?(coder _: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. override init(frame: CGRect) {
  40. super.init(frame: frame)
  41. backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
  42. addSubview(textView)
  43. addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(doneAction)))
  44. textView.snp.makeConstraints { make in
  45. make.left.equalTo(37)
  46. make.width.equalTo(300)
  47. make.height.equalTo(280)
  48. make.top.equalToSuperview().offset(134)
  49. }
  50. let doneBtn = UIButton()
  51. doneBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#389AFF")
  52. doneBtn.setTitle("option_done".BFLocale, for: .normal)
  53. doneBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
  54. doneBtn.addTarget(self, action: #selector(doneAction), for: .touchUpInside)
  55. doneBtn.addCorner(corner: 4)
  56. addSubview(doneBtn)
  57. doneBtn.snp.makeConstraints { make in
  58. make.right.equalTo(-12)
  59. make.width.equalTo(60)
  60. make.height.equalTo(36)
  61. make.top.equalToSuperview().offset(48)
  62. }
  63. let cancelBtn = UIButton()
  64. cancelBtn.backgroundColor = .clear
  65. cancelBtn.setTitle("option_cancel".BFLocale, for: .normal)
  66. cancelBtn.setTitleColor(.white, for: .normal)
  67. cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
  68. cancelBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
  69. addSubview(cancelBtn)
  70. cancelBtn.snp.makeConstraints { make in
  71. make.left.equalToSuperview().offset(12)
  72. make.width.equalTo(60)
  73. make.height.equalTo(36)
  74. make.top.equalToSuperview().offset(45)
  75. }
  76. }
  77. @objc func cancelAction() {
  78. isHidden = true
  79. textView.resignFirstResponder()
  80. }
  81. @objc func doneAction() {
  82. isHidden = true
  83. textView.resignFirstResponder()
  84. if editSubtitleDone != nil {
  85. BFLog(message: "最后修改的文字为:\(String(describing: textView.text))")
  86. editSubtitleDone!(textView.text,showSubtitleIndex)
  87. }
  88. }
  89. func setNewText(text: String,index:Int) {
  90. showSubtitleIndex = index
  91. textView.text = text
  92. // BFLog(message: "传值\(textView.text)")
  93. //
  94. // let attributedText = NSMutableAttributedString(string: textView.text,attributes: [.font: UIFont.systemFont(ofSize: CGFloat(settingModel.subtitleSize) * 375 / 1080),.strokeWidth:-6,.strokeColor: settingModel.strokeColor,.foregroundColor:settingModel.fontColor])
  95. // textView.attributedText = attributedText
  96. }
  97. }
  98. extension BFSubtitleEditView: UITextViewDelegate {
  99. func textViewDidBeginEditing(_: UITextView) {}
  100. func textViewDidEndEditing(_ textView: UITextView) {
  101. BFLog(message: "textViewDidEndEditing 输入完成 \(textView.text ?? "")")
  102. }
  103. func textView(_: UITextView, shouldChangeTextIn _: NSRange, replacementText text: String) -> Bool {
  104. if text.isEmoji() {
  105. cShowHUB(superView: nil, msg: "option_fail_emoji".BFLocale)
  106. return false
  107. }
  108. // BFLog(message: "inputText \(String(describing: textView.text))")
  109. return true
  110. }
  111. }