@@ -20,9 +20,10 @@ public final class MessageView: UIView, MessageTextViewListener {
20
20
internal var contentView : UIView ?
21
21
internal var leftButtonAction : Selector ?
22
22
internal var rightButtonAction : Selector ?
23
- public var showLeftButton : Bool = true
23
+ internal var leftButtonInset : CGFloat = 0
24
+ internal var rightButtonInset : CGFloat = 0
24
25
25
- public enum buttonType {
26
+ public enum ButtonPosition {
26
27
case left
27
28
case right
28
29
}
@@ -84,11 +85,15 @@ public final class MessageView: UIView, MessageTextViewListener {
84
85
85
86
// MARK: Public API
86
87
88
+ public var showLeftButton : Bool = true {
89
+ didSet {
90
+ delegate? . wantsLayout ( messageView: self )
91
+ }
92
+ }
93
+
87
94
public var font : UIFont ? {
88
95
get { return textView. font }
89
96
set {
90
- leftButton. titleLabel? . font = newValue
91
- rightButton. titleLabel? . font = newValue
92
97
textView. font = newValue
93
98
delegate? . wantsLayout ( messageView: self )
94
99
}
@@ -103,41 +108,37 @@ public final class MessageView: UIView, MessageTextViewListener {
103
108
}
104
109
}
105
110
106
- public var inset : UIEdgeInsets = . zero {
107
- didSet {
111
+ public var inset : UIEdgeInsets {
112
+ set {
113
+ textView. textContainerInset = newValue
108
114
setNeedsLayout ( )
109
115
delegate? . wantsLayout ( messageView: self )
110
116
}
117
+ get { return textView. textContainerInset }
111
118
}
112
119
113
- public var leftButtonInset : CGFloat = 0 {
114
- didSet { setNeedsLayout ( ) }
115
- }
116
-
117
- public func set( buttonIcon: UIImage ? , for state: UIControlState , type: buttonType ) {
120
+ public func setButton( icon: UIImage ? , for state: UIControlState , position: ButtonPosition ) {
118
121
let button : UIButton
119
- switch type {
122
+ switch position {
120
123
case . left:
121
124
button = leftButton
122
125
case . right:
123
126
button = rightButton
124
127
}
125
-
126
- button. setImage ( buttonIcon, for: state)
127
- buttonLayoutDidChange ( )
128
+ button. setImage ( icon, for: state)
129
+ buttonLayoutDidChange ( button: button)
128
130
}
129
131
130
- public func set ( buttonTitle : String , for state: UIControlState , type : buttonType ) {
132
+ public func setButton ( title : String , for state: UIControlState , position : ButtonPosition ) {
131
133
let button : UIButton
132
- switch type {
134
+ switch position {
133
135
case . left:
134
136
button = leftButton
135
137
case . right:
136
138
button = rightButton
137
139
}
138
-
139
- button. setTitle ( buttonTitle, for: state)
140
- buttonLayoutDidChange ( )
140
+ button. setTitle ( title, for: state)
141
+ buttonLayoutDidChange ( button: button)
141
142
}
142
143
143
144
public var leftButtonTint : UIColor {
@@ -177,9 +178,9 @@ public final class MessageView: UIView, MessageTextViewListener {
177
178
set { textView. keyboardType = newValue }
178
179
}
179
180
180
- public func addButton( target: Any , action: Selector , type : buttonType ) {
181
+ public func addButton( target: Any , action: Selector , position : ButtonPosition ) {
181
182
let button : UIButton
182
- switch type {
183
+ switch position {
183
184
case . left:
184
185
button = leftButton
185
186
leftButtonAction = action
@@ -196,6 +197,28 @@ public final class MessageView: UIView, MessageTextViewListener {
196
197
return [ UIKeyCommand ( input: " \r " , modifierFlags: . command, action: action) ]
197
198
}
198
199
200
+ public func setButton( inset: CGFloat , position: ButtonPosition ) {
201
+ switch position {
202
+ case . left:
203
+ leftButtonInset = inset
204
+ case . right:
205
+ rightButtonInset = inset
206
+ }
207
+ setNeedsLayout ( )
208
+ }
209
+
210
+ public func setButton( font: UIFont , position: ButtonPosition ) {
211
+ let button : UIButton
212
+ switch position {
213
+ case . left:
214
+ button = leftButton
215
+ case . right:
216
+ button = rightButton
217
+ }
218
+ button. titleLabel? . font = font
219
+ buttonLayoutDidChange ( button: button)
220
+ }
221
+
199
222
// MARK: Overrides
200
223
201
224
public override func layoutSubviews( ) {
@@ -214,33 +237,35 @@ public final class MessageView: UIView, MessageTextViewListener {
214
237
width: bounds. width - util_safeAreaInsets. left - util_safeAreaInsets. right,
215
238
height: bounds. height
216
239
)
217
- let insetBounds = UIEdgeInsetsInsetRect ( safeBounds, inset)
218
240
219
- let size = textView. font? . lineHeight ?? 25 //Use textView line height
220
- let leftButtonSize = CGSize ( width: size, height: size)
241
+ let leftButtonSize = leftButton. bounds. size
221
242
let rightButtonSize = rightButton. bounds. size
222
-
243
+
244
+ let textViewY = safeBounds. minY
245
+ let textViewHeight = self . textViewHeight
246
+ let textViewMaxY = textViewY + textViewHeight
247
+
223
248
// adjust by bottom offset so content is flush w/ text view
224
249
let leftButtonFrame = CGRect (
225
- x: insetBounds . minX,
226
- y: ( insetBounds . minY + textViewHeight ) - leftButtonSize. height + leftButton. bottomHeightOffset,
250
+ x: safeBounds . minX + inset . left ,
251
+ y: textViewMaxY - leftButtonSize. height + leftButton. bottomHeightOffset - inset . bottom ,
227
252
width: leftButtonSize. width,
228
253
height: leftButtonSize. height
229
254
)
230
- leftButton. frame = ( showLeftButton) ? leftButtonFrame : . zero
231
-
255
+ leftButton. frame = showLeftButton ? leftButtonFrame : . zero
256
+
232
257
let textViewFrame = CGRect (
233
- x: ( ( showLeftButton ) ? leftButtonFrame. maxX : 0 ) + leftButtonInset,
234
- y: insetBounds . minY ,
235
- width: insetBounds . width - ( ( showLeftButton ) ? leftButtonSize . width : 0 ) - leftButtonInset - rightButtonSize . width ,
258
+ x: leftButtonFrame. maxX + leftButtonInset,
259
+ y: textViewY ,
260
+ width: safeBounds . width - leftButtonFrame . maxX - rightButtonSize . width - rightButtonInset - inset . right ,
236
261
height: textViewHeight
237
262
)
238
263
textView. frame = textViewFrame
239
264
240
265
// adjust by bottom offset so content is flush w/ text view
241
266
let rightButtonFrame = CGRect (
242
- x: textViewFrame. maxX + leftButtonInset ,
243
- y: textViewFrame . maxY - rightButtonSize. height + rightButton. bottomHeightOffset,
267
+ x: textViewFrame. maxX + rightButtonInset ,
268
+ y: textViewMaxY - rightButtonSize. height + rightButton. bottomHeightOffset - inset . bottom ,
244
269
width: rightButtonSize. width,
245
270
height: rightButtonSize. height
246
271
)
@@ -268,14 +293,17 @@ public final class MessageView: UIView, MessageTextViewListener {
268
293
// MARK: Private API
269
294
270
295
internal var height : CGFloat {
271
- return inset. top
272
- + inset. bottom
273
- + textViewHeight
274
- + ( contentView? . bounds. height ?? 0 )
296
+ return textViewHeight + ( contentView? . bounds. height ?? 0 )
275
297
}
276
298
277
299
internal var textViewHeight : CGFloat {
278
- return min ( maxHeight, max ( textView. font? . lineHeight ?? 0 , textView. contentSize. height) )
300
+ return ceil ( min (
301
+ maxHeight,
302
+ max (
303
+ textView. font? . lineHeight ?? 0 ,
304
+ textView. contentSize. height
305
+ )
306
+ ) )
279
307
}
280
308
281
309
internal var maxHeight : CGFloat {
@@ -288,9 +316,8 @@ public final class MessageView: UIView, MessageTextViewListener {
288
316
rightButton. alpha = isEmpty ? 0.25 : 1
289
317
}
290
318
291
- internal func buttonLayoutDidChange( ) {
292
- leftButton. sizeToFit ( )
293
- rightButton. sizeToFit ( )
319
+ internal func buttonLayoutDidChange( button: UIButton ) {
320
+ button. sizeToFit ( )
294
321
setNeedsLayout ( )
295
322
}
296
323
0 commit comments