Skip to content

Commit edd79fb

Browse files
committed
Add cellTop/messageLabelTop/cellBottom case for avatar vertical position
1 parent 56418d2 commit edd79fb

4 files changed

Lines changed: 22 additions & 17 deletions

File tree

Sources/Layout/MessageSizeCalculator.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,14 @@ open class MessageSizeCalculator: CellSizeCalculator {
7676

7777
open func cellContentHeight(for message: MessageType, at indexPath: IndexPath) -> CGFloat {
7878

79-
let avatarHeight = avatarSize(for: message).height
8079
let messageContainerHeight = messageContainerSize(for: message).height
8180
let bottomLabelHeight = cellBottomLabelSize(for: message, at: indexPath).height
8281
let cellTopLabelHeight = cellTopLabelSize(for: message, at: indexPath).height
8382
let messageTopLabelHeight = messageTopLabelSize(for: message, at: indexPath).height
8483
let messageVerticalPadding = messageContainerPadding(for: message).vertical
8584

86-
var cellHeight: CGFloat = 0
87-
88-
cellHeight += max(avatarHeight, messageContainerHeight)
89-
cellHeight += messageVerticalPadding
90-
cellHeight += cellTopLabelHeight
91-
cellHeight += messageTopLabelHeight
92-
cellHeight += bottomLabelHeight
85+
let cellHeight: CGFloat = cellTopLabelHeight + messageTopLabelHeight
86+
+ messageContainerHeight + messageVerticalPadding + bottomLabelHeight
9387

9488
return cellHeight
9589
}

Sources/Layout/MessagesCollectionViewLayoutAttributes.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ open class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttribu
3737
public var messageLabelFont: UIFont = UIFont.preferredFont(forTextStyle: .body)
3838
public var messageLabelInsets: UIEdgeInsets = .zero
3939

40-
public var cellTopLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
4140
public var cellTopLabelSize: CGSize = .zero
4241

4342
public var messageTopLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
@@ -57,7 +56,6 @@ open class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttribu
5756
copy.messageContainerPadding = messageContainerPadding
5857
copy.messageLabelFont = messageLabelFont
5958
copy.messageLabelInsets = messageLabelInsets
60-
copy.cellTopLabelAlignment = cellTopLabelAlignment
6159
copy.cellTopLabelSize = cellTopLabelSize
6260
copy.messageTopLabelAlignment = messageTopLabelAlignment
6361
copy.messageTopLabelSize = messageTopLabelSize
@@ -76,7 +74,6 @@ open class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttribu
7674
&& attributes.messageContainerPadding == messageContainerPadding
7775
&& attributes.messageLabelFont == messageLabelFont
7876
&& attributes.messageLabelInsets == messageLabelInsets
79-
&& attributes.cellTopLabelAlignment == cellTopLabelAlignment
8077
&& attributes.cellTopLabelSize == cellTopLabelSize
8178
&& attributes.messageTopLabelAlignment == messageTopLabelAlignment
8279
&& attributes.messageTopLabelSize == messageTopLabelSize

Sources/Models/AvatarPosition.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,24 @@ public struct AvatarPosition {
4646
/// An enum representing the verical alignment for an `AvatarView`.
4747
public enum Vertical {
4848

49+
/// Aligns the `AvatarView`'s top edge to the cell's top edge.
50+
case cellTop
51+
52+
/// Aligns the `AvatarView`'s top edge to the `messageTopLabel`'s top edge.
53+
case messageLabelTop
54+
4955
/// Aligns the `AvatarView`'s top edge to the `MessageContainerView`'s top edge.
5056
case messageTop
5157

58+
/// Aligns the `AvatarView` center to the `MessageContainerView` center.
59+
case messageCenter
60+
5261
/// Aligns the `AvatarView`'s bottom edge to the `MessageContainerView`s bottom edge.
5362
case messageBottom
5463

55-
/// Aligns the `AvatarView` center to the `MessageContainerView` center.
56-
case messageCenter
64+
/// Aligns the `AvatarView`'s bottom edge to the cell's bottom edge.
65+
case cellBottom
66+
5767
}
5868

5969
// MARK: - Properties

Sources/Views/Cells/MessageContentCell.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ open class MessageContentCell: MessageCollectionViewCell {
4242
open var cellTopLabel: InsetLabel = {
4343
let label = InsetLabel()
4444
label.numberOfLines = 0
45+
label.textAlignment = .center
4546
return label
4647
}()
4748

@@ -94,10 +95,10 @@ open class MessageContentCell: MessageCollectionViewCell {
9495
guard let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes else { return }
9596
// Call this before other laying out other subviews
9697
layoutMessageContainerView(with: attributes)
97-
layoutAvatarView(with: attributes)
9898
layoutBottomLabel(with: attributes)
9999
layoutCellTopLabel(with: attributes)
100100
layoutMessageTopLabel(with: attributes)
101+
layoutAvatarView(with: attributes)
101102
}
102103

103104
/// Used to configure the cell.
@@ -184,12 +185,18 @@ open class MessageContentCell: MessageCollectionViewCell {
184185
}
185186

186187
switch attributes.avatarPosition.vertical {
188+
case .messageLabelTop:
189+
origin.y = messageTopLabel.frame.minY
187190
case .messageTop: // Needs messageContainerView frame to be set
188191
origin.y = messageContainerView.frame.minY
189192
case .messageBottom: // Needs messageContainerView frame to be set
190193
origin.y = messageContainerView.frame.maxY - attributes.avatarSize.height
191194
case .messageCenter: // Needs messageContainerView frame to be set
192195
origin.y = messageContainerView.frame.midY - (attributes.avatarSize.height/2)
196+
case .cellBottom:
197+
origin.y = attributes.frame.height - attributes.avatarSize.height
198+
default:
199+
break
193200
}
194201

195202
avatarView.frame = CGRect(origin: origin, size: attributes.avatarSize)
@@ -220,9 +227,6 @@ open class MessageContentCell: MessageCollectionViewCell {
220227
open func layoutCellTopLabel(with attributes: MessagesCollectionViewLayoutAttributes) {
221228
guard attributes.cellTopLabelSize != .zero else { return }
222229

223-
cellTopLabel.textAlignment = attributes.cellTopLabelAlignment.textAlignment
224-
cellTopLabel.textInsets = attributes.cellTopLabelAlignment.textInsets
225-
226230
cellTopLabel.frame = CGRect(origin: .zero, size: attributes.cellTopLabelSize)
227231
}
228232

0 commit comments

Comments
 (0)