Skip to content
Merged
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
55 changes: 21 additions & 34 deletions MessageViewController/MessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,50 +110,36 @@ public final class MessageView: UIView, MessageTextViewListener {
}
}

public var buttonLeftInset: CGFloat = 0 {
public var leftButtonInset: CGFloat = 0 {
didSet { setNeedsLayout() }
}

public func set(buttonIcon: UIImage?, for state: UIControlState, type: buttonType) {
let button: UIButton
switch type {
case .left:
setLeft(buttonIcon: buttonIcon, for: state)
break
case .right:
setRight(buttonIcon: buttonIcon, for: state)
break
case .left:

@BasThomas BasThomas Mar 14, 2018

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could even make the switch only care about setting the right button:

let button: UIButton
switch type {
case .left:
  button = leftButton
case .right
  button = rightButton
}
button.setImage(buttonIcon, for: state)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I have implemented that 👍

button = leftButton
case .right:
button = rightButton
}

button.setImage(buttonIcon, for: state)
buttonLayoutDidChange()
}

public func set(buttonTitle: String, for state: UIControlState, type: buttonType) {
let button: UIButton
switch type {
case .left:
setLeft(buttonTitle: buttonTitle, for: state)
break
button = leftButton
case .right:
setRight(buttonTitle: buttonTitle, for: state)
break
button = rightButton
}

button.setTitle(buttonTitle, for: state)
buttonLayoutDidChange()
}

private func setLeft(buttonIcon: UIImage?, for state: UIControlState) {
leftButton.setImage(buttonIcon, for: state)
}

private func setLeft(buttonTitle: String, for state: UIControlState) {
leftButton.setTitle(buttonTitle, for: state)
}

private func setRight(buttonIcon: UIImage?, for state: UIControlState) {
rightButton.setImage(buttonIcon, for: state)
}

private func setRight(buttonTitle: String, for state: UIControlState) {
rightButton.setTitle(buttonTitle, for: state)
}

public var leftButtonTint: UIColor {
get { return leftButton.tintColor }
set {
Expand Down Expand Up @@ -192,16 +178,17 @@ public final class MessageView: UIView, MessageTextViewListener {
}

public func addButton(target: Any, action: Selector, type: buttonType) {
let button: UIButton
switch type {
case .left:
leftButton.addTarget(target, action: action, for: .touchUpInside)
button = leftButton
leftButtonAction = action
break
case .right:
rightButton.addTarget(target, action: action, for: .touchUpInside)
button = rightButton
rightButtonAction = action
break
}

button.addTarget(target, action: action, for: .touchUpInside)
}

public override var keyCommands: [UIKeyCommand]? {
Expand Down Expand Up @@ -243,16 +230,16 @@ public final class MessageView: UIView, MessageTextViewListener {
leftButton.frame = (showLeftButton) ? leftButtonFrame : .zero

let textViewFrame = CGRect(
x: ((showLeftButton) ? leftButtonFrame.maxX : 0) + buttonLeftInset,
x: ((showLeftButton) ? leftButtonFrame.maxX : 0) + leftButtonInset,
y: insetBounds.minY,
width: insetBounds.width - ((showLeftButton) ? leftButtonSize.width : 0) - buttonLeftInset - rightButtonSize.width,
width: insetBounds.width - ((showLeftButton) ? leftButtonSize.width : 0) - leftButtonInset - rightButtonSize.width,
height: textViewHeight
)
textView.frame = textViewFrame

// adjust by bottom offset so content is flush w/ text view
let rightButtonFrame = CGRect(
x: textViewFrame.maxX + buttonLeftInset,
x: textViewFrame.maxX + leftButtonInset,
y: textViewFrame.maxY - rightButtonSize.height + rightButton.bottomHeightOffset,
width: rightButtonSize.width,
height: rightButtonSize.height
Expand Down