@@ -13,27 +13,44 @@ public final class MessageView: UIView, MessageTextViewListener {
13
13
public let textView = MessageTextView ( )
14
14
15
15
internal weak var delegate : MessageViewDelegate ?
16
- internal let button = UIButton ( )
16
+ internal let leftButton = UIButton ( )
17
+ internal let rightButton = UIButton ( )
17
18
internal let UITextViewContentSizeKeyPath = #keyPath( UITextView . contentSize)
18
19
internal let topBorderLayer = CALayer ( )
19
20
internal var contentView : UIView ?
20
- internal var buttonAction : Selector ?
21
+ internal var leftButtonAction : Selector ?
22
+ internal var rightButtonAction : Selector ?
23
+ public var showLeftButton : Bool = true
24
+
25
+ public enum buttonType {
26
+ case left
27
+ case right
28
+ }
21
29
22
30
internal override init ( frame: CGRect ) {
23
31
super. init ( frame: frame)
24
32
25
33
backgroundColor = . white
26
34
35
+ addSubview ( leftButton)
27
36
addSubview ( textView)
28
- addSubview ( button )
37
+ addSubview ( rightButton )
29
38
layer. addSublayer ( topBorderLayer)
30
39
40
+ //Set action button
41
+ leftButton. imageEdgeInsets = . zero
42
+ leftButton. titleEdgeInsets = . zero
43
+ leftButton. contentEdgeInsets = . zero
44
+ leftButton. titleLabel? . font = self . font ?? UIFont . systemFont ( ofSize: 14 )
45
+ leftButton. imageView? . contentMode = . scaleAspectFit
46
+ leftButton. imageView? . clipsToBounds = true
47
+
31
48
// setup text view
32
49
textView. contentInset = . zero
33
50
textView. textContainerInset = . zero
34
51
textView. backgroundColor = . clear
35
52
textView. addObserver ( self , forKeyPath: UITextViewContentSizeKeyPath, options: [ . new] , context: nil )
36
- textView. font = . systemFont( ofSize: UIFont . systemFontSize )
53
+ textView. font = self . font ?? UIFont . systemFont ( ofSize: 14 )
37
54
textView. add ( listener: self )
38
55
39
56
// setup TextKit props to defaults
@@ -47,9 +64,12 @@ public final class MessageView: UIView, MessageTextViewListener {
47
64
textView. layoutManager. usesFontLeading = true
48
65
49
66
// setup send button
50
- button. titleEdgeInsets = . zero
51
- button. contentEdgeInsets = . zero
52
- button. imageEdgeInsets = . zero
67
+ rightButton. imageEdgeInsets = . zero
68
+ rightButton. titleEdgeInsets = . zero
69
+ rightButton. contentEdgeInsets = . zero
70
+ rightButton. titleLabel? . font = self . font ?? UIFont . systemFont ( ofSize: 14 )
71
+ rightButton. imageView? . contentMode = . scaleAspectFit
72
+ rightButton. imageView? . clipsToBounds = true
53
73
54
74
updateEmptyTextStates ( )
55
75
}
@@ -67,6 +87,8 @@ public final class MessageView: UIView, MessageTextViewListener {
67
87
public var font : UIFont ? {
68
88
get { return textView. font }
69
89
set {
90
+ leftButton. titleLabel? . font = newValue
91
+ rightButton. titleLabel? . font = newValue
70
92
textView. font = newValue
71
93
delegate? . wantsLayout ( messageView: self )
72
94
}
@@ -92,22 +114,60 @@ public final class MessageView: UIView, MessageTextViewListener {
92
114
didSet { setNeedsLayout ( ) }
93
115
}
94
116
95
- public func set( buttonIcon: UIImage ? , for state: UIControlState ) {
96
- button. setImage ( buttonIcon, for: state)
117
+ public func set( buttonIcon: UIImage ? , for state: UIControlState , type: buttonType ) {
118
+ switch type {
119
+ case . left:
120
+ setLeft ( buttonIcon: buttonIcon, for: state)
121
+ break
122
+ case . right:
123
+ setRight ( buttonIcon: buttonIcon, for: state)
124
+ break
125
+ }
97
126
buttonLayoutDidChange ( )
98
127
}
99
128
100
- public func set( buttonTitle: String , for state: UIControlState ) {
101
- button. setTitle ( buttonTitle, for: state)
129
+ public func set( buttonTitle: String , for state: UIControlState , type: buttonType ) {
130
+ switch type {
131
+ case . left:
132
+ setLeft ( buttonTitle: buttonTitle, for: state)
133
+ break
134
+ case . right:
135
+ setRight ( buttonTitle: buttonTitle, for: state)
136
+ break
137
+ }
102
138
buttonLayoutDidChange ( )
103
139
}
104
140
105
- public var buttonTint : UIColor {
106
- get { return button. tintColor }
141
+ private func setLeft( buttonIcon: UIImage ? , for state: UIControlState ) {
142
+ leftButton. setImage ( buttonIcon, for: state)
143
+ }
144
+
145
+ private func setLeft( buttonTitle: String , for state: UIControlState ) {
146
+ leftButton. setTitle ( buttonTitle, for: state)
147
+ }
148
+
149
+ private func setRight( buttonIcon: UIImage ? , for state: UIControlState ) {
150
+ rightButton. setImage ( buttonIcon, for: state)
151
+ }
152
+
153
+ private func setRight( buttonTitle: String , for state: UIControlState ) {
154
+ rightButton. setTitle ( buttonTitle, for: state)
155
+ }
156
+
157
+ public var leftButtonTint : UIColor {
158
+ get { return leftButton. tintColor }
159
+ set {
160
+ leftButton. tintColor = newValue
161
+ leftButton. setTitleColor ( newValue, for: . normal)
162
+ leftButton. imageView? . tintColor = newValue
163
+ }
164
+ }
165
+ public var rightButtonTint : UIColor {
166
+ get { return rightButton. tintColor }
107
167
set {
108
- button . tintColor = newValue
109
- button . setTitleColor ( newValue, for: . normal)
110
- button . imageView? . tintColor = newValue
168
+ rightButton . tintColor = newValue
169
+ rightButton . setTitleColor ( newValue, for: . normal)
170
+ rightButton . imageView? . tintColor = newValue
111
171
}
112
172
}
113
173
@@ -131,13 +191,21 @@ public final class MessageView: UIView, MessageTextViewListener {
131
191
set { textView. keyboardType = newValue }
132
192
}
133
193
134
- public func addButton( target: Any , action: Selector ) {
135
- button. addTarget ( target, action: action, for: . touchUpInside)
136
- buttonAction = action
194
+ public func addButton( target: Any , action: Selector , type: buttonType ) {
195
+ switch type {
196
+ case . left:
197
+ leftButton. addTarget ( target, action: action, for: . touchUpInside)
198
+ leftButtonAction = action
199
+ break
200
+ case . right:
201
+ rightButton. addTarget ( target, action: action, for: . touchUpInside)
202
+ rightButtonAction = action
203
+ break
204
+ }
137
205
}
138
206
139
207
public override var keyCommands : [ UIKeyCommand ] ? {
140
- guard let action = buttonAction else { return nil }
208
+ guard let action = rightButtonAction else { return nil }
141
209
return [ UIKeyCommand ( input: " \r " , modifierFlags: . command, action: action) ]
142
210
}
143
211
@@ -161,23 +229,35 @@ public final class MessageView: UIView, MessageTextViewListener {
161
229
)
162
230
let insetBounds = UIEdgeInsetsInsetRect ( safeBounds, inset)
163
231
164
- let buttonSize = button. bounds. size
165
-
166
- let textViewFrame = CGRect (
232
+ let size = textView. font? . lineHeight ?? 25 //Use textView line height
233
+ let leftButtonSize = CGSize ( width: size, height: size)
234
+ let rightButtonSize = rightButton. bounds. size
235
+
236
+ // adjust by bottom offset so content is flush w/ text view
237
+ let leftButtonFrame = CGRect (
167
238
x: insetBounds. minX,
239
+ y: ( insetBounds. minY + textViewHeight) - leftButtonSize. height + leftButton. bottomHeightOffset,
240
+ width: leftButtonSize. width,
241
+ height: leftButtonSize. height
242
+ )
243
+ leftButton. frame = ( showLeftButton) ? leftButtonFrame : . zero
244
+
245
+ let textViewFrame = CGRect (
246
+ x: ( ( showLeftButton) ? leftButtonFrame. maxX : 0 ) + buttonLeftInset,
168
247
y: insetBounds. minY,
169
- width: insetBounds. width - buttonSize . width - buttonLeftInset,
248
+ width: insetBounds. width - ( ( showLeftButton ) ? leftButtonSize . width : 0 ) - buttonLeftInset - rightButtonSize . width ,
170
249
height: textViewHeight
171
250
)
172
251
textView. frame = textViewFrame
173
252
174
253
// adjust by bottom offset so content is flush w/ text view
175
- button . frame = CGRect (
254
+ let rightButtonFrame = CGRect (
176
255
x: textViewFrame. maxX + buttonLeftInset,
177
- y: textViewFrame. maxY - buttonSize . height + button . bottomHeightOffset,
178
- width: buttonSize . width,
179
- height: buttonSize . height
256
+ y: textViewFrame. maxY - rightButtonSize . height + rightButton . bottomHeightOffset,
257
+ width: rightButtonSize . width,
258
+ height: rightButtonSize . height
180
259
)
260
+ rightButton. frame = rightButtonFrame
181
261
182
262
let contentY = textViewFrame. maxY + inset. bottom
183
263
contentView? . frame = CGRect (
@@ -217,12 +297,13 @@ public final class MessageView: UIView, MessageTextViewListener {
217
297
218
298
internal func updateEmptyTextStates( ) {
219
299
let isEmpty = text. isEmpty
220
- button . isEnabled = !isEmpty
221
- button . alpha = isEmpty ? 0.25 : 1
300
+ rightButton . isEnabled = !isEmpty
301
+ rightButton . alpha = isEmpty ? 0.25 : 1
222
302
}
223
303
224
304
internal func buttonLayoutDidChange( ) {
225
- button. sizeToFit ( )
305
+ leftButton. sizeToFit ( )
306
+ rightButton. sizeToFit ( )
226
307
setNeedsLayout ( )
227
308
}
228
309
0 commit comments