@@ -26,76 +26,82 @@ import UIKit
2626// MARK: - NextGrowingInternalTextView: UITextView
2727
2828internal class NextGrowingInternalTextView : UITextView {
29-
30- // MARK: - Internal
31-
32- override init ( frame: CGRect , textContainer: NSTextContainer ? ) {
33- super. init ( frame: frame, textContainer: textContainer)
34-
35- NotificationCenter . default. addObserver ( self , selector: #selector( NextGrowingInternalTextView . textDidChangeNotification ( _ : ) ) , name: NSNotification . Name. UITextViewTextDidChange, object: self )
36- }
37-
38- required init ? ( coder aDecoder: NSCoder ) {
39- fatalError ( " init(coder:) has not been implemented " )
40- }
41-
42- deinit {
43- NotificationCenter . default. removeObserver ( self )
44- }
45-
46- override var text : String ! {
47- didSet {
48- self . updatePlaceholder ( )
49- }
50- }
51-
52- var placeholderAttributedText : NSAttributedString ? {
53- didSet {
54- self . setNeedsDisplay ( )
55- }
56- }
57-
58- override func layoutSubviews( ) {
59- super. layoutSubviews ( )
60- self . setNeedsDisplay ( )
61- }
62-
63- override func draw( _ rect: CGRect ) {
64-
65- super. draw ( rect)
66-
67- guard self . displayPlaceholder == true else {
68- return
69- }
70-
71- let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle ( )
72- paragraphStyle. alignment = self . textAlignment
73-
74- let targetRect = CGRect ( x: 5 + self . textContainerInset. left,
75- y: self . textContainerInset. top,
76- width: self . frame. size. width - ( self . textContainerInset. left + self . textContainerInset. right) ,
77- height: self . frame. size. height - ( self . textContainerInset. top + self . textContainerInset. bottom) )
78-
79- let attributedString = self . placeholderAttributedText
80- attributedString? . draw ( in: targetRect)
29+
30+ // MARK: - Internal
31+
32+ var didChange : ( ) -> Void = { }
33+
34+ override init ( frame: CGRect , textContainer: NSTextContainer ? ) {
35+ super. init ( frame: frame, textContainer: textContainer)
36+
37+ NotificationCenter . default. addObserver ( self , selector: #selector( NextGrowingInternalTextView . textDidChangeNotification ( _ : ) ) , name: NSNotification . Name. UITextViewTextDidChange, object: self )
38+ }
39+
40+ required init ? ( coder aDecoder: NSCoder ) {
41+ fatalError ( " init(coder:) has not been implemented " )
42+ }
43+
44+ deinit {
45+ NotificationCenter . default. removeObserver ( self )
46+ }
47+
48+ override var text : String ! {
49+ didSet {
50+ didChange ( )
51+ updatePlaceholder ( )
8152 }
82-
83- // MARK: Private
84-
85- fileprivate var displayPlaceholder : Bool = true {
86- didSet {
87- if oldValue != self . displayPlaceholder {
88- self . setNeedsDisplay ( )
89- }
90- }
53+ }
54+
55+ var placeholderAttributedText : NSAttributedString ? {
56+ didSet {
57+ setNeedsDisplay ( )
9158 }
92-
93- fileprivate dynamic func textDidChangeNotification( _ notification: Notification ) {
94-
95- self . updatePlaceholder ( )
59+ }
60+
61+ override func layoutSubviews( ) {
62+ super. layoutSubviews ( )
63+ setNeedsDisplay ( )
64+ }
65+
66+ override func draw( _ rect: CGRect ) {
67+
68+ super. draw ( rect)
69+
70+ guard displayPlaceholder == true else {
71+ return
9672 }
73+
74+ let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle ( )
75+ paragraphStyle. alignment = textAlignment
76+
77+ let targetRect = CGRect (
78+ x: 5 + textContainerInset. left,
79+ y: textContainerInset. top,
80+ width: frame. size. width - ( textContainerInset. left + textContainerInset. right) ,
81+ height: frame. size. height - ( textContainerInset. top + textContainerInset. bottom)
82+ )
9783
98- fileprivate func updatePlaceholder( ) {
99- self . displayPlaceholder = self . text. characters. count == 0
84+ let attributedString = placeholderAttributedText
85+ attributedString? . draw ( in: targetRect)
86+ }
87+
88+ // MARK: Private
89+
90+ private var displayPlaceholder : Bool = true {
91+ didSet {
92+ if oldValue != displayPlaceholder {
93+ setNeedsDisplay ( )
94+ }
10095 }
96+ }
97+
98+ private dynamic func textDidChangeNotification( _ notification: Notification ) {
99+
100+ updatePlaceholder ( )
101+ didChange ( )
102+ }
103+
104+ private func updatePlaceholder( ) {
105+ displayPlaceholder = text. characters. count == 0
106+ }
101107}
0 commit comments