@@ -13,21 +13,34 @@ 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
+
24
+ public enum buttonType {
25
+ case left
26
+ case right
27
+ }
21
28
22
29
internal override init ( frame: CGRect ) {
23
30
super. init ( frame: frame)
24
31
25
32
backgroundColor = . white
26
33
34
+ addSubview ( leftButton)
27
35
addSubview ( textView)
28
- addSubview ( button )
36
+ addSubview ( rightButton )
29
37
layer. addSublayer ( topBorderLayer)
30
38
39
+ //Set action button
40
+ leftButton. imageEdgeInsets = . zero
41
+ leftButton. titleEdgeInsets = . zero
42
+ leftButton. contentEdgeInsets = . zero
43
+
31
44
// setup text view
32
45
textView. contentInset = . zero
33
46
textView. textContainerInset = . zero
@@ -47,9 +60,9 @@ public final class MessageView: UIView, MessageTextViewListener {
47
60
textView. layoutManager. usesFontLeading = true
48
61
49
62
// setup send button
50
- button . titleEdgeInsets = . zero
51
- button . contentEdgeInsets = . zero
52
- button . imageEdgeInsets = . zero
63
+ rightButton . imageEdgeInsets = . zero
64
+ rightButton . titleEdgeInsets = . zero
65
+ rightButton . contentEdgeInsets = . zero
53
66
54
67
updateEmptyTextStates ( )
55
68
}
@@ -92,22 +105,62 @@ public final class MessageView: UIView, MessageTextViewListener {
92
105
didSet { setNeedsLayout ( ) }
93
106
}
94
107
95
- public func set( buttonIcon: UIImage ? , for state: UIControlState ) {
96
- button. setImage ( buttonIcon, for: state)
108
+ public func set( buttonIcon: UIImage ? , for state: UIControlState , type: buttonType ) {
109
+ switch type {
110
+ case . left:
111
+ setLeft ( buttonIcon: buttonIcon, for: state)
112
+ break
113
+ case . right:
114
+ setRight ( buttonIcon: buttonIcon, for: state)
115
+ break
116
+ }
117
+ }
118
+
119
+ public func set( buttonTitle: String , for state: UIControlState , type: buttonType ) {
120
+ switch type {
121
+ case . left:
122
+ setLeft ( buttonTitle: buttonTitle, for: state)
123
+ break
124
+ case . right:
125
+ setRight ( buttonTitle: buttonTitle, for: state)
126
+ break
127
+ }
128
+ }
129
+
130
+ private func setLeft( buttonIcon: UIImage ? , for state: UIControlState ) {
131
+ leftButton. setImage ( buttonIcon, for: state)
97
132
buttonLayoutDidChange ( )
98
133
}
99
134
100
- public func set ( buttonTitle: String , for state: UIControlState ) {
101
- button . setTitle ( buttonTitle, for: state)
135
+ private func setLeft ( buttonTitle: String , for state: UIControlState ) {
136
+ leftButton . setTitle ( buttonTitle, for: state)
102
137
buttonLayoutDidChange ( )
103
138
}
104
139
105
- public var buttonTint : UIColor {
106
- get { return button. tintColor }
140
+ private func setRight( buttonIcon: UIImage ? , for state: UIControlState ) {
141
+ rightButton. setImage ( buttonIcon, for: state)
142
+ buttonLayoutDidChange ( )
143
+ }
144
+
145
+ private func setRight( buttonTitle: String , for state: UIControlState ) {
146
+ rightButton. setTitle ( buttonTitle, for: state)
147
+ buttonLayoutDidChange ( )
148
+ }
149
+
150
+ public var leftButtonTint : UIColor {
151
+ get { return leftButton. tintColor }
152
+ set {
153
+ leftButton. tintColor = newValue
154
+ leftButton. setTitleColor ( newValue, for: . normal)
155
+ leftButton. imageView? . tintColor = newValue
156
+ }
157
+ }
158
+ public var rightButtonTint : UIColor {
159
+ get { return rightButton. tintColor }
107
160
set {
108
- button . tintColor = newValue
109
- button . setTitleColor ( newValue, for: . normal)
110
- button . imageView? . tintColor = newValue
161
+ rightButton . tintColor = newValue
162
+ rightButton . setTitleColor ( newValue, for: . normal)
163
+ rightButton . imageView? . tintColor = newValue
111
164
}
112
165
}
113
166
@@ -132,12 +185,12 @@ public final class MessageView: UIView, MessageTextViewListener {
132
185
}
133
186
134
187
public func addButton( target: Any , action: Selector ) {
135
- button . addTarget ( target, action: action, for: . touchUpInside)
136
- buttonAction = action
188
+ rightButton . addTarget ( target, action: action, for: . touchUpInside)
189
+ rightButtonAction = action
137
190
}
138
191
139
192
public override var keyCommands : [ UIKeyCommand ] ? {
140
- guard let action = buttonAction else { return nil }
193
+ guard let action = rightButtonAction else { return nil }
141
194
return [ UIKeyCommand ( input: " \r " , modifierFlags: . command, action: action) ]
142
195
}
143
196
@@ -161,23 +214,34 @@ public final class MessageView: UIView, MessageTextViewListener {
161
214
)
162
215
let insetBounds = UIEdgeInsetsInsetRect ( safeBounds, inset)
163
216
164
- let buttonSize = button. bounds. size
217
+ let leftButtonSize = leftButton. bounds. size
218
+ let rightButtonSize = rightButton. bounds. size
219
+
220
+ // adjust by bottom offset so content is flush w/ text view
221
+ let leftButtonFrame = CGRect (
222
+ x: insetBounds. minX,
223
+ y: insetBounds. minY,
224
+ width: leftButtonSize. width,
225
+ height: leftButtonSize. height
226
+ )
227
+ leftButton. frame = leftButtonFrame
165
228
166
229
let textViewFrame = CGRect (
167
- x: insetBounds . minX ,
230
+ x: leftButtonFrame . maxX + buttonLeftInset ,
168
231
y: insetBounds. minY,
169
- width: insetBounds. width - buttonSize . width - buttonLeftInset,
232
+ width: insetBounds. width - leftButtonSize . width - buttonLeftInset - rightButtonSize . width ,
170
233
height: textViewHeight
171
234
)
172
235
textView. frame = textViewFrame
173
236
174
237
// adjust by bottom offset so content is flush w/ text view
175
- button . frame = CGRect (
238
+ let rightButtonFrame = CGRect (
176
239
x: textViewFrame. maxX + buttonLeftInset,
177
- y: textViewFrame. maxY - buttonSize . height + button . bottomHeightOffset,
178
- width: buttonSize . width,
179
- height: buttonSize . height
240
+ y: textViewFrame. maxY - rightButtonSize . height + rightButton . bottomHeightOffset,
241
+ width: rightButtonSize . width,
242
+ height: rightButtonSize . height
180
243
)
244
+ rightButton. frame = rightButtonFrame
181
245
182
246
let contentY = textViewFrame. maxY + inset. bottom
183
247
contentView? . frame = CGRect (
@@ -217,12 +281,13 @@ public final class MessageView: UIView, MessageTextViewListener {
217
281
218
282
internal func updateEmptyTextStates( ) {
219
283
let isEmpty = text. isEmpty
220
- button . isEnabled = !isEmpty
221
- button . alpha = isEmpty ? 0.25 : 1
284
+ rightButton . isEnabled = !isEmpty
285
+ rightButton . alpha = isEmpty ? 0.25 : 1
222
286
}
223
287
224
288
internal func buttonLayoutDidChange( ) {
225
- button. sizeToFit ( )
289
+ leftButton. sizeToFit ( )
290
+ rightButton. sizeToFit ( )
226
291
setNeedsLayout ( )
227
292
}
228
293
0 commit comments