Skip to content

Commit cc65d5b

Browse files
authored
Multiline placeholders (#95)
* - Cleanup - Update demo to iOS 9 min (support for StackView) - Add a demo of an embedd growing text view in a scrollview * Allow multiline placeholders
1 parent 49f4bc0 commit cc65d5b

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

NextGrowingTextView.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@
8686
4BDB87F91DC0426A00E70D5B /* Demo */,
8787
4BDB87E61DC0423F00E70D5B /* Products */,
8888
);
89+
indentWidth = 2;
8990
sourceTree = "<group>";
91+
tabWidth = 2;
9092
};
9193
4BDB87E61DC0423F00E70D5B /* Products */ = {
9294
isa = PBXGroup;

NextGrowingTextView/NextGrowingInternalTextView.swift

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ internal class NextGrowingInternalTextView: UITextView {
3131

3232
var didChange: () -> Void = {}
3333
var didUpdateHeightDependencies: () -> Void = {}
34-
34+
3535
private lazy var placeholderDisplayLabel = UILabel()
3636

3737
override init(frame: CGRect, textContainer: NSTextContainer?) {
3838
super.init(frame: frame, textContainer: textContainer)
3939

4040
NotificationCenter.default.addObserver(self, selector: #selector(NextGrowingInternalTextView.textDidChangeNotification(_ :)), name: UITextView.textDidChangeNotification, object: self)
41-
42-
placeholderDisplayLabel.numberOfLines = 0
43-
placeholderDisplayLabel.adjustsFontSizeToFitWidth = true
41+
isPlaceHolderMultiLine = false
4442
placeholderDisplayLabel.minimumScaleFactor = 0.4
43+
placeholderDisplayLabel.lineBreakMode = .byWordWrapping
4544
addSubview(placeholderDisplayLabel)
4645
}
4746

@@ -78,7 +77,23 @@ internal class NextGrowingInternalTextView: UITextView {
7877
didUpdateHeightDependencies()
7978
}
8079
}
81-
80+
81+
var isPlaceHolderMultiLine: Bool {
82+
get {
83+
placeholderDisplayLabel.numberOfLines != 1
84+
}
85+
set {
86+
if newValue {
87+
placeholderDisplayLabel.numberOfLines = 1
88+
placeholderDisplayLabel.adjustsFontSizeToFitWidth = true
89+
} else {
90+
placeholderDisplayLabel.numberOfLines = 0
91+
placeholderDisplayLabel.adjustsFontSizeToFitWidth = false
92+
}
93+
updatePlaceholder()
94+
}
95+
}
96+
8297
var placeholderAttributedText: NSAttributedString? {
8398
get {
8499
placeholderDisplayLabel.attributedText
@@ -92,19 +107,18 @@ internal class NextGrowingInternalTextView: UITextView {
92107
override func layoutSubviews() {
93108
super.layoutSubviews()
94109

95-
let maxSize = bounds.inset(by: textContainerInset).size
96-
97-
var size = placeholderDisplayLabel.sizeThatFits(maxSize)
98-
size.height = min(size.height, maxSize.height)
99-
110+
let boundsInsettedSize = bounds.inset(by: textContainerInset).size
111+
var size = placeholderDisplayLabel.sizeThatFits(boundsInsettedSize)
112+
if !isPlaceHolderMultiLine {
113+
size.height = min(boundsInsettedSize.height, size.height)
114+
}
100115
placeholderDisplayLabel.frame = CGRect(
101116
origin: .init(
102117
x: 5 + textContainerInset.left,
103118
y: textContainerInset.top
104119
),
105120
size: size
106121
)
107-
108122
}
109123

110124
// MARK: Private

NextGrowingTextView/NextGrowingTextView.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ open class NextGrowingTextView: UIScrollView {
8080
get { return _textView.placeholderAttributedText }
8181
set { _textView.placeholderAttributedText = newValue }
8282
}
83-
83+
84+
/// true: Let the placeholder spans any number of lines. The view must be tall enough to contain it.
85+
/// false: placeholder will shring by up to 0.4 if it cannot fit one line.
86+
open var isPlacholderMultiline: Bool {
87+
get { _textView.isPlaceHolderMultiLine }
88+
set { _textView.isPlaceHolderMultiLine = newValue }
89+
}
90+
8491
open override var inputView: UIView? {
8592
get {
8693
return _textView.inputView

0 commit comments

Comments
 (0)