Skip to content

Commit

Permalink
Initial text renders with placeholder (Bug fix):
Browse files Browse the repository at this point in the history
If text was set intially after placeholder, placeholder visiblity is not updated. Therefore, textview renders with both placeholder and text value.
  • Loading branch information
aliirshaid committed May 23, 2024
1 parent 7611cbb commit 71b42c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ extension JNMentionTextView: UITextViewDelegate {
self.mentionDelegate?.textViewDidChange?(textView)

// Update placeholder label visibility
self.showPlaceholder(textView.text.isEmpty)
self.updatePlaceholderLabelVisibility()
}

/**
Expand All @@ -195,7 +195,7 @@ extension JNMentionTextView: UITextViewDelegate {
self.mentionDelegate?.textViewDidBeginEditing?(textView)

// Update placeholder label visibility
self.showPlaceholder(textView.text.isEmpty)
self.updatePlaceholderLabelVisibility()
}

/**
Expand All @@ -209,7 +209,7 @@ extension JNMentionTextView: UITextViewDelegate {
}

// Update placeholder label visibility
self.showPlaceholder(textView.text.isEmpty)
self.updatePlaceholderLabelVisibility()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ public class JNMentionTextView: UITextView {
}
}

/// Text
override open var text: String! {
didSet {
self.updatePlaceholderLabelVisibility()
}
}

/// Attributed text
override open var attributedText: NSAttributedString! {
didSet {
self.updatePlaceholderLabelVisibility()
}
}

/// Font
public override var font: UIFont? {
didSet {
Expand Down Expand Up @@ -179,10 +193,26 @@ public class JNMentionTextView: UITextView {
}

/**
Update placeholder label visibility
Update placeholder visibility
*/
public func showPlaceholder(_ show: Bool) {
self.placeholderLabel?.isHidden = !show
public func updatePlaceholderLabelVisibility() {
guard self.placeholderLabel != nil else { return }

// Is placeholder hidden
var isPlaceholderLabelHidden = true

// Check if there's a placeholder
// AND value text is empty
if let placeholder = self.placeHolder,
!placeholder.isEmpty,
self.text.isEmpty,
self.attributedText.string.isEmpty {

isPlaceholderLabelHidden = false
}

// Update placeholder label visibility
self.placeholderLabel?.isHidden = isPlaceholderLabelHidden
}

/**
Expand Down Expand Up @@ -212,6 +242,9 @@ public class JNMentionTextView: UITextView {

// Add subview
self.addSubview(self.placeholderLabel!)

// Update placeholder label visibility
self.updatePlaceholderLabelVisibility()
}

/**
Expand Down

0 comments on commit 71b42c9

Please sign in to comment.