TSPlaceholderTextView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // TSPlaceholderTextView.swift
  3. // Pods
  4. //
  5. // Created by 100Years on 2025/3/4.
  6. //
  7. open class TSPlaceholderTextView: UITextView {
  8. // Placeholder Label
  9. public lazy var placeholderLabel: TopLeftLabel = {
  10. let placeholderLabel = TopLeftLabel()
  11. placeholderLabel.font = font
  12. placeholderLabel.textColor = placeholderColor
  13. placeholderLabel.text = placeholder
  14. placeholderLabel.numberOfLines = 0
  15. placeholderLabel.isUserInteractionEnabled = false
  16. placeholderLabel.textAlignment = .left
  17. return placeholderLabel
  18. }()
  19. public var maxLength:Int = 1000 {
  20. didSet {
  21. handleTextViewTextCount(textView: self)
  22. }
  23. }
  24. // Placeholder Text
  25. public var placeholder: String? {
  26. didSet {
  27. placeholderLabel.text = placeholder
  28. }
  29. }
  30. // Placeholder Color
  31. public var placeholderColor: UIColor = .lightGray {
  32. didSet {
  33. placeholderLabel.textColor = placeholderColor
  34. }
  35. }
  36. // Placeholder Insets
  37. public var textInsets: UIEdgeInsets = .zero {
  38. didSet {
  39. updateTextContainerInset()
  40. }
  41. }
  42. // Override text property to manage placeholder visibility
  43. open override var text: String! {
  44. didSet {
  45. updatePlaceholderVisibility()
  46. }
  47. }
  48. public var textViewTextChanged:(()->Void)?
  49. // Initializer
  50. public init(placeholder: String? = nil,
  51. text: String? = nil,
  52. font: UIFont? = nil,
  53. textColor: UIColor = .black,
  54. backgroundColor: UIColor = .white,
  55. textInsets: UIEdgeInsets = .zero) {
  56. super.init(frame: .zero, textContainer: nil)
  57. self.placeholder = placeholder
  58. self.font = font
  59. self.textColor = textColor
  60. self.backgroundColor = backgroundColor
  61. self.textInsets = textInsets
  62. setupPlaceholder()
  63. self.text = text
  64. }
  65. required public init?(coder: NSCoder) {
  66. super.init(coder: coder)
  67. setupPlaceholder()
  68. }
  69. // Setup placeholder label
  70. private func setupPlaceholder() {
  71. addSubview(placeholderLabel)
  72. updatePlaceholderVisibility()
  73. // Observe text changes to update placeholder
  74. NotificationCenter.default.addObserver(self,
  75. selector: #selector(textDidChange(_:)),
  76. name: UITextView.textDidChangeNotification,
  77. object: self)
  78. // Update text container insets
  79. updateTextContainerInset()
  80. }
  81. deinit {
  82. NotificationCenter.default.removeObserver(self)
  83. }
  84. // Update placeholder visibility
  85. private func updatePlaceholderVisibility() {
  86. placeholderLabel.isHidden = !text.isEmpty
  87. }
  88. @objc private func textDidChange(_ notification: Notification) {
  89. if let textView = notification.object as? UITextView {
  90. handleTextViewTextCount(textView: textView)
  91. textViewTextChanged?()
  92. }
  93. updatePlaceholderVisibility()
  94. }
  95. open func handleTextViewTextCount(textView:UITextView) {
  96. if textView.text.count > maxLength {
  97. textView.text = String(textView.text.prefix(maxLength))
  98. }
  99. }
  100. // Update textContainerInset and placeholder frame
  101. private func updateTextContainerInset() {
  102. textContainerInset = textInsets
  103. setNeedsLayout()
  104. }
  105. // Layout placeholder
  106. open override func layoutSubviews() {
  107. super.layoutSubviews()
  108. let placeholderX = textInsets.left + 5 // Add padding for placeholder
  109. let placeholderY = textInsets.top
  110. let placeholderWidth = bounds.width - textInsets.left - textInsets.right - 10 // Adjust for padding
  111. let placeholderHeight = bounds.height - textInsets.top - textInsets.bottom
  112. // if let font = font {
  113. // placeholderHeight = placeholder?.height(ofFont: font, maxWidth: k_ScreenWidth-64)
  114. // }
  115. placeholderLabel.frame = CGRect(x: placeholderX,
  116. y: placeholderY,
  117. width: placeholderWidth,
  118. height: placeholderHeight)
  119. }
  120. }