@@ -20,9 +20,10 @@ public final class MessageView: UIView, MessageTextViewListener {
2020 internal var contentView : UIView ?
2121 internal var leftButtonAction : Selector ?
2222 internal var rightButtonAction : Selector ?
23- public var showLeftButton : Bool = true
23+ internal var leftButtonInset : CGFloat = 0
24+ internal var rightButtonInset : CGFloat = 0
2425
25- public enum buttonType {
26+ public enum ButtonPosition {
2627 case left
2728 case right
2829 }
@@ -84,11 +85,15 @@ public final class MessageView: UIView, MessageTextViewListener {
8485
8586 // MARK: Public API
8687
88+ public var showLeftButton : Bool = true {
89+ didSet {
90+ delegate? . wantsLayout ( messageView: self )
91+ }
92+ }
93+
8794 public var font : UIFont ? {
8895 get { return textView. font }
8996 set {
90- leftButton. titleLabel? . font = newValue
91- rightButton. titleLabel? . font = newValue
9297 textView. font = newValue
9398 delegate? . wantsLayout ( messageView: self )
9499 }
@@ -103,41 +108,37 @@ public final class MessageView: UIView, MessageTextViewListener {
103108 }
104109 }
105110
106- public var inset : UIEdgeInsets = . zero {
107- didSet {
111+ public var inset : UIEdgeInsets {
112+ set {
113+ textView. textContainerInset = newValue
108114 setNeedsLayout ( )
109115 delegate? . wantsLayout ( messageView: self )
110116 }
117+ get { return textView. textContainerInset }
111118 }
112119
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 ) {
118121 let button : UIButton
119- switch type {
122+ switch position {
120123 case . left:
121124 button = leftButton
122125 case . right:
123126 button = rightButton
124127 }
125-
126- button. setImage ( buttonIcon, for: state)
127- buttonLayoutDidChange ( )
128+ button. setImage ( icon, for: state)
129+ buttonLayoutDidChange ( button: button)
128130 }
129131
130- public func set ( buttonTitle : String , for state: UIControlState , type : buttonType ) {
132+ public func setButton ( title : String , for state: UIControlState , position : ButtonPosition ) {
131133 let button : UIButton
132- switch type {
134+ switch position {
133135 case . left:
134136 button = leftButton
135137 case . right:
136138 button = rightButton
137139 }
138-
139- button. setTitle ( buttonTitle, for: state)
140- buttonLayoutDidChange ( )
140+ button. setTitle ( title, for: state)
141+ buttonLayoutDidChange ( button: button)
141142 }
142143
143144 public var leftButtonTint : UIColor {
@@ -177,9 +178,9 @@ public final class MessageView: UIView, MessageTextViewListener {
177178 set { textView. keyboardType = newValue }
178179 }
179180
180- public func addButton( target: Any , action: Selector , type : buttonType ) {
181+ public func addButton( target: Any , action: Selector , position : ButtonPosition ) {
181182 let button : UIButton
182- switch type {
183+ switch position {
183184 case . left:
184185 button = leftButton
185186 leftButtonAction = action
@@ -196,6 +197,28 @@ public final class MessageView: UIView, MessageTextViewListener {
196197 return [ UIKeyCommand ( input: " \r " , modifierFlags: . command, action: action) ]
197198 }
198199
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+
199222 // MARK: Overrides
200223
201224 public override func layoutSubviews( ) {
@@ -214,33 +237,35 @@ public final class MessageView: UIView, MessageTextViewListener {
214237 width: bounds. width - util_safeAreaInsets. left - util_safeAreaInsets. right,
215238 height: bounds. height
216239 )
217- let insetBounds = UIEdgeInsetsInsetRect ( safeBounds, inset)
218240
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
221242 let rightButtonSize = rightButton. bounds. size
222-
243+
244+ let textViewY = safeBounds. minY
245+ let textViewHeight = self . textViewHeight
246+ let textViewMaxY = textViewY + textViewHeight
247+
223248 // adjust by bottom offset so content is flush w/ text view
224249 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 ,
227252 width: leftButtonSize. width,
228253 height: leftButtonSize. height
229254 )
230- leftButton. frame = ( showLeftButton) ? leftButtonFrame : . zero
231-
255+ leftButton. frame = showLeftButton ? leftButtonFrame : . zero
256+
232257 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 ,
236261 height: textViewHeight
237262 )
238263 textView. frame = textViewFrame
239264
240265 // adjust by bottom offset so content is flush w/ text view
241266 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 ,
244269 width: rightButtonSize. width,
245270 height: rightButtonSize. height
246271 )
@@ -268,14 +293,17 @@ public final class MessageView: UIView, MessageTextViewListener {
268293 // MARK: Private API
269294
270295 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 )
275297 }
276298
277299 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+ ) )
279307 }
280308
281309 internal var maxHeight : CGFloat {
@@ -288,9 +316,8 @@ public final class MessageView: UIView, MessageTextViewListener {
288316 rightButton. alpha = isEmpty ? 0.25 : 1
289317 }
290318
291- internal func buttonLayoutDidChange( ) {
292- leftButton. sizeToFit ( )
293- rightButton. sizeToFit ( )
319+ internal func buttonLayoutDidChange( button: UIButton ) {
320+ button. sizeToFit ( )
294321 setNeedsLayout ( )
295322 }
296323
0 commit comments