123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //
- // TSPlaceholderTextView.swift
- // Pods
- //
- // Created by 100Years on 2025/3/4.
- //
- open class TSPlaceholderTextView: UITextView {
-
- // Placeholder Label
- public lazy var placeholderLabel: TopLeftLabel = {
- let placeholderLabel = TopLeftLabel()
- placeholderLabel.font = font
- placeholderLabel.textColor = placeholderColor
- placeholderLabel.text = placeholder
- placeholderLabel.numberOfLines = 0
- placeholderLabel.isUserInteractionEnabled = false
- placeholderLabel.textAlignment = .left
- return placeholderLabel
- }()
-
- public var maxLength:Int = 1000 {
- didSet {
- handleTextViewTextCount(textView: self)
- }
- }
- // Placeholder Text
- public var placeholder: String? {
- didSet {
- placeholderLabel.text = placeholder
- }
- }
-
- // Placeholder Color
- public var placeholderColor: UIColor = .lightGray {
- didSet {
- placeholderLabel.textColor = placeholderColor
- }
- }
-
- // Placeholder Insets
- public var textInsets: UIEdgeInsets = .zero {
- didSet {
- updateTextContainerInset()
- }
- }
-
- // Override text property to manage placeholder visibility
- open override var text: String! {
- didSet {
- updatePlaceholderVisibility()
- }
- }
-
- public var textViewTextChanged:(()->Void)?
-
- // Initializer
- public init(placeholder: String? = nil,
- text: String? = nil,
- font: UIFont? = nil,
- textColor: UIColor = .black,
- backgroundColor: UIColor = .white,
- textInsets: UIEdgeInsets = .zero) {
- super.init(frame: .zero, textContainer: nil)
-
- self.placeholder = placeholder
- self.font = font
- self.textColor = textColor
- self.backgroundColor = backgroundColor
- self.textInsets = textInsets
-
- setupPlaceholder()
- self.text = text
- }
-
- required public init?(coder: NSCoder) {
- super.init(coder: coder)
- setupPlaceholder()
- }
-
- // Setup placeholder label
- private func setupPlaceholder() {
-
- addSubview(placeholderLabel)
- updatePlaceholderVisibility()
-
- // Observe text changes to update placeholder
- NotificationCenter.default.addObserver(self,
- selector: #selector(textDidChange(_:)),
- name: UITextView.textDidChangeNotification,
- object: self)
-
- // Update text container insets
- updateTextContainerInset()
- }
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
- // Update placeholder visibility
- private func updatePlaceholderVisibility() {
- placeholderLabel.isHidden = !text.isEmpty
- }
-
-
- @objc private func textDidChange(_ notification: Notification) {
-
- if let textView = notification.object as? UITextView {
- handleTextViewTextCount(textView: textView)
- textViewTextChanged?()
- }
- updatePlaceholderVisibility()
- }
-
- open func handleTextViewTextCount(textView:UITextView) {
- if textView.text.count > maxLength {
- textView.text = String(textView.text.prefix(maxLength))
- }
- }
-
- // Update textContainerInset and placeholder frame
- private func updateTextContainerInset() {
- textContainerInset = textInsets
- setNeedsLayout()
- }
-
- // Layout placeholder
- open override func layoutSubviews() {
- super.layoutSubviews()
-
-
- let placeholderX = textInsets.left + 5 // Add padding for placeholder
- let placeholderY = textInsets.top
- let placeholderWidth = bounds.width - textInsets.left - textInsets.right - 10 // Adjust for padding
- let placeholderHeight = bounds.height - textInsets.top - textInsets.bottom
-
-
- // if let font = font {
- // placeholderHeight = placeholder?.height(ofFont: font, maxWidth: k_ScreenWidth-64)
- // }
-
- placeholderLabel.frame = CGRect(x: placeholderX,
- y: placeholderY,
- width: placeholderWidth,
- height: placeholderHeight)
- }
- }
|