TSPromptTextView.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // TSTGPTextView.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/4.
  6. //
  7. class TSPromptTextView : TSBaseView{
  8. var randomTextArray:[String]
  9. var textChangedBlock:((String)->Void)
  10. init(randomTextArray: [String], textChangedBlock: @escaping (String) -> Void) {
  11. self.randomTextArray = randomTextArray
  12. self.textChangedBlock = textChangedBlock
  13. super.init(frame: .zero)
  14. }
  15. @MainActor required init?(coder: NSCoder) {
  16. fatalError("init(coder:) has not been implemented")
  17. }
  18. lazy var titleView: TSTGPTitleView = {
  19. let titleView = TSTGPTitleView()
  20. titleView.titleLab.text = "Enter a prompt".localized
  21. return titleView
  22. }()
  23. lazy var randomTextPicker: TSRandomTextPicker = {
  24. let textPicker = TSRandomTextPicker(texts: randomTextArray)
  25. return textPicker
  26. }()
  27. lazy var textBgView: UIView = {
  28. let textBgView = UIView()
  29. let bgImageView = UIImageView.createImageView(imageName: "textView_bg",contentMode: .scaleToFill)
  30. textBgView.addSubview(bgImageView)
  31. bgImageView.snp.makeConstraints { make in
  32. make.top.equalTo(0)
  33. make.centerX.equalToSuperview()
  34. make.width.equalTo(343*kDesignScale)
  35. make.height.equalTo(160*kDesignScale)
  36. }
  37. return textBgView
  38. }()
  39. lazy var customTextView: TSPlaceholderTextView = {
  40. let customTextView = TSPlaceholderTextView(
  41. placeholder: "Type your idea here.",
  42. text: "",
  43. font: .font(size: 16),
  44. textColor: .white,
  45. backgroundColor: .clear
  46. )
  47. customTextView.delegate = self
  48. return customTextView
  49. }()
  50. lazy var inspirationBtn: UIButton = {
  51. let inspirationBtn = UIButton.createButton(
  52. title: "Hint Inspiration".localized,
  53. image: UIImage(named: "textView_hint"),
  54. font: .font(size: 14),
  55. titleColor: .themeColor
  56. )
  57. { [weak self] in
  58. guard let self = self else { return }
  59. customTextView.text = randomTextPicker.getRandomText()
  60. textViewDidChange(customTextView)
  61. }
  62. inspirationBtn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 40, bottom: 4, right: 50)
  63. inspirationBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
  64. return inspirationBtn
  65. }()
  66. lazy var clearBtn: TSUIExpandedTouchButton = {
  67. let clearBtn = TSUIExpandedTouchButton()
  68. clearBtn.setUpButton(image: UIImage(named: "clear_text"))
  69. { [weak self] in
  70. guard let self = self else { return }
  71. customTextView.text = ""
  72. textViewDidChange(customTextView)
  73. }
  74. clearBtn.setImage(UIImage(named: "clear_gary_text"), for: .disabled)
  75. clearBtn.isEnabled = false
  76. return clearBtn
  77. }()
  78. let maxNum:Int = 1000
  79. var currenMun:Int = 0 {
  80. didSet{
  81. currenMunLab.text = "\(currenMun)"
  82. }
  83. }
  84. lazy var currenMunLab: UILabel = {
  85. let numLab = UILabel.createLabel(text: "\(currenMun)",font: .font(name: .PingFangSC,size: 12),textColor: .white)
  86. return numLab
  87. }()
  88. lazy var numView: UIView = {
  89. let numView = UIView()
  90. numView.addSubview(currenMunLab)
  91. let numLab = UILabel.createLabel(text: "/\(maxNum)",font: .font(name: .PingFangSC,size: 12),textColor: .white.withAlphaComponent(0.4))
  92. numView.addSubview(numLab)
  93. let lineView = UIView()
  94. lineView.cornerRadius = 0.5
  95. lineView.backgroundColor = .white
  96. numView.addSubview(lineView)
  97. currenMunLab.snp.makeConstraints { make in
  98. make.centerY.equalToSuperview()
  99. make.leading.equalTo(0)
  100. }
  101. numLab.snp.makeConstraints { make in
  102. make.centerY.equalToSuperview()
  103. make.leading.equalTo(currenMunLab.snp.trailing)
  104. }
  105. lineView.snp.makeConstraints { make in
  106. make.top.trailing.bottom.equalToSuperview()
  107. make.leading.equalTo(numLab.snp.trailing).offset(8)
  108. make.width.equalTo(1)
  109. make.height.equalTo(12)
  110. }
  111. return numView
  112. }()
  113. override func creatUI() {
  114. let titleViewH = titleView.viewH
  115. addSubview(titleView)
  116. titleView.snp.makeConstraints { make in
  117. make.top.equalTo(0)
  118. make.leading.trailing.equalTo(0)
  119. make.height.equalTo(titleViewH)
  120. }
  121. contentView.snp.updateConstraints { make in
  122. make.top.equalTo(titleViewH)
  123. }
  124. contentView.addSubview(textBgView)
  125. textBgView.snp.makeConstraints { make in
  126. make.top.equalTo(0)
  127. make.leading.equalTo(16)
  128. make.trailing.equalTo(-16)
  129. make.height.equalTo(160.0*kDesignScale)
  130. make.bottom.equalTo(-10)
  131. }
  132. textBgView.addSubview(customTextView)
  133. customTextView.snp.makeConstraints { make in
  134. make.top.equalTo(16)
  135. make.leading.equalTo(16)
  136. make.trailing.equalTo(-16)
  137. make.bottom.equalTo(-48)
  138. }
  139. textBgView.addSubview(inspirationBtn)
  140. inspirationBtn.snp.makeConstraints { make in
  141. make.height.equalTo(32)
  142. make.bottom.equalTo(0)
  143. make.leading.equalTo(0)
  144. }
  145. textBgView.addSubview(clearBtn)
  146. clearBtn.snp.makeConstraints { make in
  147. make.width.height.equalTo(14)
  148. make.bottom.equalTo(-14)
  149. make.trailing.equalTo(-14)
  150. }
  151. textBgView.addSubview(numView)
  152. numView.snp.makeConstraints { make in
  153. make.centerY.equalTo(clearBtn)
  154. make.trailing.equalTo(clearBtn.snp.leading).offset(-8)
  155. }
  156. }
  157. func getVipText()->String{
  158. return "Generate"
  159. // if kPurchaseDefault.isVip {
  160. // return "Generate"
  161. // }
  162. // return "Generate (\(kPurchaseDefault.freeNum(type: .generatePic)))"
  163. }
  164. }
  165. extension TSPromptTextView: UITextViewDelegate{
  166. func textViewDidBeginEditing(_ textView: UITextView) {
  167. // self.colComponent?.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredVertically, animated: true)
  168. }
  169. func textViewDidChange(_ textView: UITextView) {
  170. currenMun = textView.text.count
  171. clearBtn.isEnabled = currenMun > 0
  172. textChangedBlock(textView.text)
  173. }
  174. }