Skip to content

Commit 2b530d9

Browse files
authored
Merge pull request #80 from muukii/muukii/placeholder-label
Use UILabel to display placeholder
2 parents aa9203e + 55d797b commit 2b530d9

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

Demo/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ViewController: UIViewController {
4040
self.growingTextView.layer.cornerRadius = 4
4141
self.growingTextView.backgroundColor = UIColor(white: 0.9, alpha: 1)
4242
self.growingTextView.placeholderAttributedText = NSAttributedString(
43-
string: "Placeholder text",
43+
string: "Placeholder text Placeholder text Placeholder text",
4444
attributes: [
4545
.font: self.growingTextView.textView.font!,
4646
.foregroundColor: UIColor.gray

NextGrowingTextView.xcodeproj/project.pbxproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
developmentRegion = English;
199199
hasScannedForEncodings = 0;
200200
knownRegions = (
201+
English,
201202
en,
202203
Base,
203204
);
@@ -320,7 +321,7 @@
320321
SDKROOT = iphoneos;
321322
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
322323
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
323-
SWIFT_VERSION = 4.2;
324+
SWIFT_VERSION = 5.0;
324325
TARGETED_DEVICE_FAMILY = "1,2";
325326
VERSIONING_SYSTEM = "apple-generic";
326327
VERSION_INFO_PREFIX = "";
@@ -366,7 +367,7 @@
366367
MTL_ENABLE_DEBUG_INFO = NO;
367368
SDKROOT = iphoneos;
368369
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
369-
SWIFT_VERSION = 4.2;
370+
SWIFT_VERSION = 5.0;
370371
TARGETED_DEVICE_FAMILY = "1,2";
371372
VALIDATE_PRODUCT = YES;
372373
VERSIONING_SYSTEM = "apple-generic";

NextGrowingTextView/NextGrowingInternalTextView.swift

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ internal class NextGrowingInternalTextView: UITextView {
3131

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

3537
override init(frame: CGRect, textContainer: NSTextContainer?) {
3638
super.init(frame: frame, textContainer: textContainer)
3739

3840
NotificationCenter.default.addObserver(self, selector: #selector(NextGrowingInternalTextView.textDidChangeNotification(_ :)), name: UITextView.textDidChangeNotification, object: self)
41+
42+
placeholderDisplayLabel.numberOfLines = 0
43+
placeholderDisplayLabel.adjustsFontSizeToFitWidth = true
44+
placeholderDisplayLabel.minimumScaleFactor = 0.4
45+
addSubview(placeholderDisplayLabel)
3946
}
4047

4148
required init?(coder aDecoder: NSCoder) {
@@ -71,54 +78,44 @@ internal class NextGrowingInternalTextView: UITextView {
7178
didUpdateHeightDependencies()
7279
}
7380
}
74-
81+
7582
var placeholderAttributedText: NSAttributedString? {
76-
didSet {
77-
setNeedsDisplay()
83+
get {
84+
placeholderDisplayLabel.attributedText
85+
}
86+
set {
87+
placeholderDisplayLabel.attributedText = newValue
88+
setNeedsLayout()
7889
}
7990
}
8091

8192
override func layoutSubviews() {
8293
super.layoutSubviews()
83-
setNeedsDisplay()
84-
}
85-
86-
override func draw(_ rect: CGRect) {
87-
super.draw(rect)
88-
89-
guard displayPlaceholder else { return }
90-
91-
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
92-
paragraphStyle.alignment = textAlignment
93-
94-
let targetRect = CGRect(
95-
x: 5 + textContainerInset.left,
96-
y: textContainerInset.top,
97-
width: frame.size.width - (textContainerInset.left + textContainerInset.right),
98-
height: frame.size.height - (textContainerInset.top + textContainerInset.bottom)
94+
95+
let maxSize = bounds.inset(by: textContainerInset).size
96+
97+
var size = placeholderDisplayLabel.sizeThatFits(maxSize)
98+
size.height = min(size.height, maxSize.height)
99+
100+
placeholderDisplayLabel.frame = CGRect(
101+
origin: .init(
102+
x: 5 + textContainerInset.left,
103+
y: textContainerInset.top
104+
),
105+
size: size
99106
)
100107

101-
let attributedString = placeholderAttributedText
102-
attributedString?.draw(in: targetRect)
103108
}
104109

105110
// MARK: Private
106111

107-
private var displayPlaceholder: Bool = true {
108-
didSet {
109-
if oldValue != displayPlaceholder {
110-
setNeedsDisplay()
111-
}
112-
}
113-
}
114-
115112
@objc
116113
private dynamic func textDidChangeNotification(_ notification: Notification) {
117114
updatePlaceholder()
118115
didChange()
119116
}
120117

121118
private func updatePlaceholder() {
122-
displayPlaceholder = text.isEmpty
119+
placeholderDisplayLabel.isHidden = !text.isEmpty
123120
}
124121
}

0 commit comments

Comments
 (0)