Skip to content

Commit

Permalink
Fixed multiple line placeholder showing issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
onl1ner committed Aug 11, 2020
1 parent 4dc5751 commit fe587e1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Binary file not shown.
57 changes: 54 additions & 3 deletions STTextView/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,31 @@ import UIKit
textView.isUserInteractionEnabled = false
textView.translatesAutoresizingMaskIntoConstraints = false

textView.isHidden = !self.text.isEmpty

return textView
}()

private var heightConstraint : NSLayoutConstraint?
private var originHeightConstant : CGFloat?

@objc private func textDidBeginEditing(_ notification : Notification) -> () {
if self.text.isEmpty {
if shouldHidePlaceholderOnEditing {
placeholderTextView.isHidden = true
}
}
updateContentSize()
}

@objc private func textDidChange(_ notification : Notification) -> () {
placeholderTextView.isHidden = !self.text.isEmpty
if shouldHidePlaceholderOnEditing {
if self.text.isEmpty {
placeholderTextView.isHidden = true
}
} else {
placeholderTextView.isHidden = !self.text.isEmpty
}
}

@objc private func textDidEndEditing(_ notification : Notification) -> () {
Expand All @@ -103,16 +115,44 @@ import UIKit
placeholderTextView.isHidden = false
}
}
updateContentSize()
}

// Method is used to update the placeholderTextView whenever
// the TextView changes in Interface Builder.
// the UITextView changes in Interface Builder.
private func updatePlaceholder() -> () {
placeholderTextView.text = placeholder
placeholderTextView.textColor = placeholderColor

placeholderTextView.font = self.font
placeholderTextView.textAlignment = self.textAlignment

placeholderTextView.frame = self.bounds
}

// The content should be always visible
// even if it's just a placeholder text, so
// this function is solving the problem when a placeholder text
// did not fit in the UITextView if the height constraint's constant
// is less than the placeholder text.
private func updateContentSize() -> () {
if self.text.isEmpty {
if let constraint = heightConstraint, let originHeight = originHeightConstant {
let placeholderContentHeight = placeholderTextView.contentSize.height

if shouldHidePlaceholderOnEditing && isFirstResponder {
if originHeight < placeholderContentHeight {
constraint.constant = originHeight
}
} else {
if constraint.constant < placeholderContentHeight {
constraint.constant = placeholderContentHeight
}
}
}
}

placeholderTextView.frame = self.bounds
}

private func signForNotifications() -> () {
Expand All @@ -136,7 +176,18 @@ import UIKit
public override func layoutSubviews() {
super.layoutSubviews()

placeholderTextView.frame = self.bounds
// Getting an initial value of the height constraint's constant
if heightConstraint == nil {
for constraint in self.constraints {
if constraint.firstAttribute == .height {
heightConstraint = constraint
originHeightConstant = constraint.constant
break
}
}
}

updateContentSize()
}

public override init(frame: CGRect, textContainer: NSTextContainer?) {
Expand Down
Binary file not shown.

0 comments on commit fe587e1

Please sign in to comment.