Skip to content

Commit d985cf5

Browse files
Kashish Goelrnystrom
Kashish Goel
authored andcommitted
Dynamically increase textview size according to maxScreenRatio (#39)
* Added screen ratio height customization * Add logic to account for view insets * Remove unused vars * Add ability to set maxHeight of messageView
1 parent b41b119 commit d985cf5

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

MessageViewController/MessageView.swift

+41-22
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ public final class MessageView: UIView, MessageTextViewListener {
2222
internal var rightButtonAction: Selector?
2323
internal var leftButtonInset: CGFloat = 0
2424
internal var rightButtonInset: CGFloat = 0
25-
25+
internal var ignoreLineHeight = false
26+
2627
public enum ButtonPosition {
2728
case left
2829
case right
2930
}
3031

32+
internal var heightOffset: CGFloat = 0
33+
3134
internal override init(frame: CGRect) {
3235
super.init(frame: frame)
3336

@@ -163,13 +166,27 @@ public final class MessageView: UIView, MessageTextViewListener {
163166
rightButton.imageView?.tintColor = newValue
164167
}
165168
}
166-
169+
170+
public var maxHeight: CGFloat = CGFloat.greatestFiniteMagnitude {
171+
didSet {
172+
delegate?.wantsLayout(messageView: self)
173+
}
174+
}
175+
167176
public var maxLineCount: Int = 4 {
168177
didSet {
178+
ignoreLineHeight = maxLineHeight == 0
169179
delegate?.wantsLayout(messageView: self)
170180
}
171181
}
172182

183+
public var maxScreenRatio: CGFloat = 1 {
184+
didSet {
185+
maxScreenRatio = 0...1 ~= maxScreenRatio ? maxScreenRatio : 0
186+
delegate?.wantsLayout(messageView: self)
187+
}
188+
}
189+
173190
public func add(contentView: UIView) {
174191
self.contentView?.removeFromSuperview()
175192
assert(contentView.bounds.height > 0, "Must have a non-zero content height")
@@ -291,47 +308,49 @@ public final class MessageView: UIView, MessageTextViewListener {
291308
textViewContentSizeDidChange()
292309
}
293310
}
294-
311+
295312
public override func resignFirstResponder() -> Bool {
296313
return textView.resignFirstResponder()
297314
}
298-
315+
299316
// MARK: Private API
300-
317+
301318
internal var height: CGFloat {
302319
return textViewHeight + (contentView?.bounds.height ?? 0)
303320
}
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 {
316323
return (font?.lineHeight ?? 0) * CGFloat(maxLineCount)
317324
}
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+
319338
internal func updateEmptyTextStates() {
320339
let isEmpty = text.isEmpty
321340
rightButton.isEnabled = !isEmpty
322341
rightButton.alpha = isEmpty ? 0.25 : 1
323342
}
324-
343+
325344
internal func buttonLayoutDidChange(button: UIButton) {
326345
button.sizeToFit()
327346
setNeedsLayout()
328347
}
329-
348+
330349
internal func textViewContentSizeDidChange() {
331350
delegate?.sizeDidChange(messageView: self)
332-
textView.alwaysBounceVertical = textView.contentSize.height > maxHeight
351+
textView.alwaysBounceVertical = textView.contentSize.height > calculatedMaxHeight
333352
}
334-
353+
335354
// MARK: MessageTextViewListener
336355

337356
public func didChange(textView: MessageTextView) {

MessageViewController/MessageViewController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ open class MessageViewController: UIViewController, MessageAutocompleteControlle
166166

167167
let previousKeyboardHeight = keyboardHeight
168168
keyboardHeight = keyboardFrame.height
169-
169+
messageView.heightOffset = keyboardHeight + (scrollView?.util_safeAreaInsets.top ?? 0)
170+
170171
UIView.animate(withDuration: animationDuration) {
171172
guard let scrollView = self.scrollView else { return }
172173
// capture before changing the frame which might have weird side effects

0 commit comments

Comments
 (0)