@@ -2,104 +2,102 @@ import UIKit
2
2
3
3
/**
4
4
A `UITextView` subclass that automatically resizes based on the size of its content text with a smooth animation.
5
- */
5
+ */
6
6
open class ResizingTextView : UITextView {
7
-
8
- /*
9
- * MARK: - Instance Properties
10
- */
11
-
12
- /// The duration of the animation while resizing the text view in seconds. Defaults to `0.1`.
13
- var resizeDuration : TimeInterval = 0.1
14
-
15
- fileprivate var heightConstraint : NSLayoutConstraint !
16
-
17
- /*
18
- * MARK: - Object Lifecycle
19
- */
20
-
21
- override init ( frame: CGRect , textContainer: NSTextContainer ? ) {
22
- super. init ( frame: frame, textContainer: textContainer)
23
-
24
- commonInit ( )
25
- }
26
-
27
- public required init ? ( coder aDecoder: NSCoder ) {
28
- super. init ( coder: aDecoder)
29
-
30
- commonInit ( )
31
- }
32
-
33
- fileprivate func commonInit( ) {
34
- attachHeightConstraint ( )
35
- registerForTextChangeNotification ( )
36
- }
37
-
38
- fileprivate func attachHeightConstraint( ) {
39
- if !findHeightConstraint( ) {
40
- createHeightConstraint ( )
7
+
8
+ /*
9
+ * MARK: - Instance Properties
10
+ */
11
+
12
+ /// The duration of the animation while resizing the text view in seconds. Defaults to `0.1`.
13
+ var resizeDuration : TimeInterval = 0.1
14
+
15
+ fileprivate var heightConstraint : NSLayoutConstraint !
16
+
17
+ /*
18
+ * MARK: - Object Lifecycle
19
+ */
20
+
21
+ override init ( frame: CGRect , textContainer: NSTextContainer ? ) {
22
+ super. init ( frame: frame, textContainer: textContainer)
23
+
24
+ commonInit ( )
25
+ }
26
+
27
+ public required init ? ( coder aDecoder: NSCoder ) {
28
+ super. init ( coder: aDecoder)
29
+
30
+ commonInit ( )
31
+ }
32
+
33
+ fileprivate func commonInit( ) {
34
+ attachHeightConstraint ( )
35
+ registerForTextChangeNotification ( )
36
+ }
37
+
38
+ fileprivate func attachHeightConstraint( ) {
39
+ if !findHeightConstraint( ) {
40
+ createHeightConstraint ( )
41
+ }
42
+ }
43
+
44
+ fileprivate func findHeightConstraint( ) -> Bool {
45
+ for constraint in constraints {
46
+ if constraint. firstAttribute == . height {
47
+ heightConstraint = constraint
48
+ return true
49
+ }
50
+ }
51
+ return false
52
+ }
53
+
54
+ fileprivate func createHeightConstraint( ) {
55
+ heightConstraint = NSLayoutConstraint ( item: self ,
56
+ attribute: . height,
57
+ relatedBy: . equal,
58
+ toItem: nil ,
59
+ attribute: . notAnAttribute,
60
+ multiplier: 1.0 ,
61
+ constant: heightForCurrentText ( ) )
62
+
63
+ addConstraint ( heightConstraint)
41
64
}
42
- }
43
-
44
- fileprivate func findHeightConstraint( ) -> Bool {
45
- for constraint in constraints {
46
- if constraint. firstAttribute == . height {
47
- heightConstraint = constraint
48
- return true
49
- }
65
+
66
+ fileprivate func heightForCurrentText( ) -> CGFloat {
67
+ return sizeThatFits ( self . frame. size) . height
50
68
}
51
- return false
52
- }
53
-
54
- fileprivate func createHeightConstraint( ) {
55
- heightConstraint = NSLayoutConstraint ( item: self ,
56
- attribute: . height,
57
- relatedBy: . equal,
58
- toItem: nil ,
59
- attribute: . notAnAttribute,
60
- multiplier: 1.0 ,
61
- constant: heightForCurrentText ( ) )
62
-
63
- addConstraint ( heightConstraint)
64
- }
65
-
66
- fileprivate func heightForCurrentText( ) -> CGFloat {
67
- return sizeThatFits ( self . frame. size) . height
68
- }
69
-
70
- fileprivate func registerForTextChangeNotification( ) {
71
- NotificationCenter . default. addObserver ( self ,
72
- selector: #selector( ResizingTextView . didUpdateText ( _: ) ) ,
73
- name: NSNotification . Name. UITextViewTextDidChange,
74
- object: self )
75
- }
76
-
77
- deinit {
78
- NotificationCenter . default. removeObserver ( self )
79
- }
80
-
81
- /*
82
- * MARK: - Auto Resizing
83
- */
84
-
85
- func didUpdateText( _ sender: AnyObject ) {
86
- let newHeight = heightForCurrentText ( )
87
- if newHeight != heightConstraint. constant {
88
- updateHeightConstraint ( newHeight)
69
+
70
+ fileprivate func registerForTextChangeNotification( ) {
71
+ NotificationCenter . default. addObserver ( self ,
72
+ selector: #selector( ResizingTextView . didUpdateText ( _: ) ) ,
73
+ name: NSNotification . Name. UITextViewTextDidChange,
74
+ object: self )
75
+ }
76
+
77
+ deinit {
78
+ NotificationCenter . default. removeObserver ( self )
89
79
}
90
- }
91
-
92
- fileprivate func updateHeightConstraint( _ newHeight: CGFloat ) {
93
- heightConstraint. constant = newHeight
94
- setNeedsLayout ( )
95
-
96
- UIView . animate ( withDuration: resizeDuration,
97
- delay: 0 ,
98
- options: . layoutSubviews,
99
- animations: { ( ) -> Void in
100
- self . layoutIfNeeded ( )
101
- } ,
102
- completion: nil )
103
- }
104
-
80
+
81
+ /*
82
+ * MARK: - Auto Resizing
83
+ */
84
+
85
+ func didUpdateText( _ sender: AnyObject ) {
86
+ let newHeight = heightForCurrentText ( )
87
+ if newHeight != heightConstraint. constant {
88
+ updateHeightConstraint ( newHeight)
89
+ }
90
+ }
91
+
92
+ fileprivate func updateHeightConstraint( _ newHeight: CGFloat ) {
93
+ heightConstraint. constant = newHeight
94
+ setNeedsLayout ( )
95
+
96
+ UIView . animate ( withDuration: resizeDuration,
97
+ delay: 0 ,
98
+ options: . layoutSubviews,
99
+ animations: { self . layoutIfNeeded ( ) } ,
100
+ completion: nil )
101
+ }
102
+
105
103
}
0 commit comments