TSPromptTextView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // TSPromptTextView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/3/11.
  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 robotImageView: UIImageView = {
  19. let robotImageView = UIImageView.createImageView(imageName: "aichat_avatar")
  20. return robotImageView
  21. }()
  22. lazy var randomTextPicker: TSRandomTextPicker = {
  23. let textPicker = TSRandomTextPicker(texts: kRandomTextArray)
  24. return textPicker
  25. }()
  26. lazy var textBgView: UIView = {
  27. let textBgView = UIView()
  28. textBgView.backgroundColor = "#333333".uiColor
  29. textBgView.cornerRadius = 12
  30. return textBgView
  31. }()
  32. lazy var customTextView: TSPlaceholderTextView = {
  33. let customTextView = TSPlaceholderTextView(
  34. placeholder: "Type your idea here.".localized,
  35. text: "",
  36. font: .font(size: 14),
  37. textColor: .white,
  38. backgroundColor: "#333333".uiColor
  39. )
  40. customTextView.delegate = self
  41. customTextView.returnKeyType = .send
  42. return customTextView
  43. }()
  44. lazy var inspirationBtn: UIButton = {
  45. let inspirationBtn = UIButton.createButton(
  46. title: "Hint Inspiration".localized,
  47. image: UIImage(named: "inspiration_yellow"),
  48. backgroundColor:"#FECB34".uiColor.withAlphaComponent(0.1),
  49. font: .font(size: 12),
  50. titleColor: .themeColor,
  51. corner: 8.0)
  52. { [weak self] in
  53. guard let self = self else { return }
  54. customTextView.text = randomTextPicker.getRandomText()
  55. textViewDidChange(customTextView)
  56. }
  57. inspirationBtn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7)
  58. inspirationBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -4, bottom: 0, right: 0)
  59. inspirationBtn.isHidden = false
  60. return inspirationBtn
  61. }()
  62. lazy var clearBtn: UIButton = {
  63. let clearBtn = UIButton.createButton(
  64. image: UIImage(named: "clear_text")
  65. )
  66. { [weak self] in
  67. guard let self = self else { return }
  68. customTextView.text = ""
  69. textViewDidChange(customTextView)
  70. }
  71. clearBtn.isHidden = true
  72. return clearBtn
  73. }()
  74. override func creatUI() {
  75. contentView.addSubview(robotImageView)
  76. robotImageView.snp.makeConstraints { make in
  77. make.top.equalTo(0)
  78. make.leading.equalTo(16)
  79. make.width.height.equalTo(82)
  80. }
  81. contentView.addSubview(textBgView)
  82. textBgView.snp.makeConstraints { make in
  83. make.top.equalTo(57)
  84. make.leading.equalTo(16)
  85. make.trailing.equalTo(-16)
  86. make.height.equalTo(182.0)
  87. // make.height.equalTo(182.0*kDesignScale)
  88. // make.bottom.equalTo(-20)
  89. make.bottom.equalTo(0)
  90. }
  91. textBgView.addSubview(customTextView)
  92. customTextView.snp.makeConstraints { make in
  93. make.top.equalTo(21)
  94. make.leading.equalTo(16)
  95. make.trailing.equalTo(-16)
  96. make.bottom.equalTo(-21)
  97. }
  98. textBgView.addSubview(inspirationBtn)
  99. inspirationBtn.snp.makeConstraints { make in
  100. make.height.equalTo(28)
  101. make.bottom.equalTo(-16)
  102. make.leading.equalTo(16)
  103. }
  104. textBgView.addSubview(clearBtn)
  105. clearBtn.snp.makeConstraints { make in
  106. make.width.height.equalTo(16)
  107. make.bottom.equalTo(-16)
  108. make.trailing.equalTo(-16)
  109. }
  110. }
  111. func getVipText()->String{
  112. return "Generate".localized
  113. // if kPurchaseDefault.isVip {
  114. // return "Generate"
  115. // }
  116. // return "Generate (\(kPurchaseDefault.freeNum(type: .generatePic)))"
  117. }
  118. }
  119. extension TSPromptTextView: UITextViewDelegate{
  120. // func textViewDidBeginEditing(_ textView: UITextView) {
  121. // self.colComponent?.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredVertically, animated: true)
  122. // }
  123. func textViewDidChange(_ textView: UITextView) {
  124. let text = textView.text.replacingOccurrences(of: " ", with: "")
  125. clearBtn.isHidden = text.count > 0 ? false : true
  126. textChangedBlock(textView.text)
  127. }
  128. }