BFSubtitleEditView.swift 4.3 KB

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