@@ -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