Skip to content

Dynamically increase textview size according to maxScreenRatio #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions MessageViewController/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ public final class MessageView: UIView, MessageTextViewListener {
internal var rightButtonAction: Selector?
internal var leftButtonInset: CGFloat = 0
internal var rightButtonInset: CGFloat = 0

internal var ignoreLineHeight = false

public enum ButtonPosition {
case left
case right
}

internal var heightOffset: CGFloat = 0

internal override init(frame: CGRect) {
super.init(frame: frame)

Expand Down Expand Up @@ -157,13 +160,27 @@ public final class MessageView: UIView, MessageTextViewListener {
rightButton.imageView?.tintColor = newValue
}
}


public var maxHeight: CGFloat = CGFloat.greatestFiniteMagnitude {
didSet {
delegate?.wantsLayout(messageView: self)
}
}

public var maxLineCount: Int = 4 {
didSet {
ignoreLineHeight = maxLineHeight == 0
delegate?.wantsLayout(messageView: self)
}
}

public var maxScreenRatio: CGFloat = 1 {
didSet {
maxScreenRatio = 0...1 ~= maxScreenRatio ? maxScreenRatio : 0
delegate?.wantsLayout(messageView: self)
}
}

public func add(contentView: UIView) {
self.contentView?.removeFromSuperview()
assert(contentView.bounds.height > 0, "Must have a non-zero content height")
Expand Down Expand Up @@ -285,47 +302,49 @@ public final class MessageView: UIView, MessageTextViewListener {
textViewContentSizeDidChange()
}
}

public override func resignFirstResponder() -> Bool {
return textView.resignFirstResponder()
}

// MARK: Private API

internal var height: CGFloat {
return textViewHeight + (contentView?.bounds.height ?? 0)
}

internal var textViewHeight: CGFloat {
return ceil(min(
maxHeight,
max(
textView.font?.lineHeight ?? 0,
textView.contentSize.height
)
))
}

internal var maxHeight: CGFloat {

internal var maxLineHeight: CGFloat {
return (font?.lineHeight ?? 0) * CGFloat(maxLineCount)
}


internal var maxScreenRatioHeight: CGFloat {
return maxScreenRatio * ((superview?.frame.height ?? 0) - heightOffset)
}

internal var calculatedMaxHeight: CGFloat {
return ignoreLineHeight == true ? min(maxScreenRatioHeight, maxHeight) : min(maxScreenRatioHeight, maxLineHeight, maxHeight)
}

internal var textViewHeight: CGFloat {
return ceil(min(calculatedMaxHeight, textView.contentSize.height))
}

internal func updateEmptyTextStates() {
let isEmpty = text.isEmpty
rightButton.isEnabled = !isEmpty
rightButton.alpha = isEmpty ? 0.25 : 1
}

internal func buttonLayoutDidChange(button: UIButton) {
button.sizeToFit()
setNeedsLayout()
}

internal func textViewContentSizeDidChange() {
delegate?.sizeDidChange(messageView: self)
textView.alwaysBounceVertical = textView.contentSize.height > maxHeight
textView.alwaysBounceVertical = textView.contentSize.height > calculatedMaxHeight
}

// MARK: MessageTextViewListener

public func didChange(textView: MessageTextView) {
Expand Down
3 changes: 2 additions & 1 deletion MessageViewController/MessageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ open class MessageViewController: UIViewController, MessageAutocompleteControlle

let previousKeyboardHeight = keyboardHeight
keyboardHeight = keyboardFrame.height

messageView.heightOffset = keyboardHeight + (scrollView?.util_safeAreaInsets.top ?? 0)

UIView.animate(withDuration: animationDuration) {
guard let scrollView = self.scrollView else { return }
// capture before changing the frame which might have weird side effects
Expand Down