@@ -22,12 +22,15 @@ public final class MessageView: UIView, MessageTextViewListener {
22
22
internal var rightButtonAction : Selector ?
23
23
internal var leftButtonInset : CGFloat = 0
24
24
internal var rightButtonInset : CGFloat = 0
25
-
25
+ internal var ignoreLineHeight = false
26
+
26
27
public enum ButtonPosition {
27
28
case left
28
29
case right
29
30
}
30
31
32
+ internal var heightOffset : CGFloat = 0
33
+
31
34
internal override init ( frame: CGRect ) {
32
35
super. init ( frame: frame)
33
36
@@ -163,13 +166,27 @@ public final class MessageView: UIView, MessageTextViewListener {
163
166
rightButton. imageView? . tintColor = newValue
164
167
}
165
168
}
166
-
169
+
170
+ public var maxHeight : CGFloat = CGFloat . greatestFiniteMagnitude {
171
+ didSet {
172
+ delegate? . wantsLayout ( messageView: self )
173
+ }
174
+ }
175
+
167
176
public var maxLineCount : Int = 4 {
168
177
didSet {
178
+ ignoreLineHeight = maxLineHeight == 0
169
179
delegate? . wantsLayout ( messageView: self )
170
180
}
171
181
}
172
182
183
+ public var maxScreenRatio : CGFloat = 1 {
184
+ didSet {
185
+ maxScreenRatio = 0 ... 1 ~= maxScreenRatio ? maxScreenRatio : 0
186
+ delegate? . wantsLayout ( messageView: self )
187
+ }
188
+ }
189
+
173
190
public func add( contentView: UIView ) {
174
191
self . contentView? . removeFromSuperview ( )
175
192
assert ( contentView. bounds. height > 0 , " Must have a non-zero content height " )
@@ -291,47 +308,49 @@ public final class MessageView: UIView, MessageTextViewListener {
291
308
textViewContentSizeDidChange ( )
292
309
}
293
310
}
294
-
311
+
295
312
public override func resignFirstResponder( ) -> Bool {
296
313
return textView. resignFirstResponder ( )
297
314
}
298
-
315
+
299
316
// MARK: Private API
300
-
317
+
301
318
internal var height : CGFloat {
302
319
return textViewHeight + ( contentView? . bounds. height ?? 0 )
303
320
}
304
-
305
- internal var textViewHeight : CGFloat {
306
- return ceil ( min (
307
- maxHeight,
308
- max (
309
- textView. font? . lineHeight ?? 0 ,
310
- textView. contentSize. height
311
- )
312
- ) )
313
- }
314
-
315
- internal var maxHeight : CGFloat {
321
+
322
+ internal var maxLineHeight : CGFloat {
316
323
return ( font? . lineHeight ?? 0 ) * CGFloat( maxLineCount)
317
324
}
318
-
325
+
326
+ internal var maxScreenRatioHeight : CGFloat {
327
+ return maxScreenRatio * ( ( superview? . frame. height ?? 0 ) - heightOffset)
328
+ }
329
+
330
+ internal var calculatedMaxHeight : CGFloat {
331
+ return ignoreLineHeight == true ? min ( maxScreenRatioHeight, maxHeight) : min ( maxScreenRatioHeight, maxLineHeight, maxHeight)
332
+ }
333
+
334
+ internal var textViewHeight : CGFloat {
335
+ return ceil ( min ( calculatedMaxHeight, textView. contentSize. height) )
336
+ }
337
+
319
338
internal func updateEmptyTextStates( ) {
320
339
let isEmpty = text. isEmpty
321
340
rightButton. isEnabled = !isEmpty
322
341
rightButton. alpha = isEmpty ? 0.25 : 1
323
342
}
324
-
343
+
325
344
internal func buttonLayoutDidChange( button: UIButton ) {
326
345
button. sizeToFit ( )
327
346
setNeedsLayout ( )
328
347
}
329
-
348
+
330
349
internal func textViewContentSizeDidChange( ) {
331
350
delegate? . sizeDidChange ( messageView: self )
332
- textView. alwaysBounceVertical = textView. contentSize. height > maxHeight
351
+ textView. alwaysBounceVertical = textView. contentSize. height > calculatedMaxHeight
333
352
}
334
-
353
+
335
354
// MARK: MessageTextViewListener
336
355
337
356
public func didChange( textView: MessageTextView ) {
0 commit comments