diff --git a/CHANGELOG.md b/CHANGELOG.md index eb04451f..ab6503c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa ## Upcoming release +### Added + +- Added convenience method to set properties for all `MessageSizeCalculator`s. +[#697](https://github.com/MessageKit/MessageKit/pull/697) by [@zhongwuzw](https://github.com/zhongwuzw). + ## [[Prerelease] 1.0.0-beta.1](https://github.com/MessageKit/MessageKit/releases/tag/1.0.0-beta.1) ### Added diff --git a/Sources/Layout/MessagesCollectionViewFlowLayout.swift b/Sources/Layout/MessagesCollectionViewFlowLayout.swift index a3ee26a7..c34f5e0f 100644 --- a/Sources/Layout/MessagesCollectionViewFlowLayout.swift +++ b/Sources/Layout/MessagesCollectionViewFlowLayout.swift @@ -33,7 +33,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { return MessagesCollectionViewLayoutAttributes.self } - /** The `MessagesCollectionView` that owns this layout object. */ + /// The `MessagesCollectionView` that owns this layout object. public var messagesCollectionView: MessagesCollectionView { guard let messagesCollectionView = collectionView as? MessagesCollectionView else { fatalError(MessageKitError.layoutUsedOnForeignType) @@ -41,7 +41,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { return messagesCollectionView } - /** The `MessagesDataSource` for the layout's collection view. */ + /// The `MessagesDataSource` for the layout's collection view. public var messagesDataSource: MessagesDataSource { guard let messagesDataSource = messagesCollectionView.messagesDataSource else { fatalError(MessageKitError.nilMessagesDataSource) @@ -49,7 +49,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { return messagesDataSource } - /** The `MessagesLayoutDelegate` for the layout's collection view. */ + /// The `MessagesLayoutDelegate` for the layout's collection view. public var messagesLayoutDelegate: MessagesLayoutDelegate { guard let messagesLayoutDelegate = messagesCollectionView.messagesLayoutDelegate else { fatalError(MessageKitError.nilMessagesLayoutDelegate) @@ -155,4 +155,70 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout { let calculator = cellSizeCalculatorForItem(at: indexPath) return calculator.sizeForItem(at: indexPath) } + + /// Set `incomingAvatarSize` of all `MessageSizeCalculator`s + public func setMessageIncomingAvatarSize(_ newSize: CGSize) { + messageSizeCalculators().forEach { $0.incomingAvatarSize = newSize } + } + + /// Set `outgoingAvatarSize` of all `MessageSizeCalculator`s + public func setMessageOutgoingAvatarSize(_ newSize: CGSize) { + messageSizeCalculators().forEach { $0.outgoingAvatarSize = newSize } + } + + /// Set `incomingAvatarPosition` of all `MessageSizeCalculator`s + public func setMessageIncomingAvatarPosition(_ newPosition: AvatarPosition) { + messageSizeCalculators().forEach { $0.incomingAvatarPosition = newPosition } + } + + /// Set `outgoingAvatarPosition` of all `MessageSizeCalculator`s + public func setMessageOutgoingAvatarPosition(_ newPosition: AvatarPosition) { + messageSizeCalculators().forEach { $0.outgoingAvatarPosition = newPosition } + } + + /// Set `incomingMessagePadding` of all `MessageSizeCalculator`s + public func setMessageIncomingMessagePadding(_ newPadding: UIEdgeInsets) { + messageSizeCalculators().forEach { $0.incomingMessagePadding = newPadding } + } + + /// Set `outgoingMessagePadding` of all `MessageSizeCalculator`s + public func setMessageOutgoingMessagePadding(_ newPadding: UIEdgeInsets) { + messageSizeCalculators().forEach { $0.outgoingMessagePadding = newPadding } + } + + /// Set `incomingCellTopLabelAlignment` of all `MessageSizeCalculator`s + public func setMessageIncomingCellTopLabelAlignment(_ newAlignment: LabelAlignment) { + messageSizeCalculators().forEach { $0.incomingCellTopLabelAlignment = newAlignment } + } + + /// Set `outgoingCellTopLabelAlignment` of all `MessageSizeCalculator`s + public func setMessageOutgoingCellTopLabelAlignment(_ newAlignment: LabelAlignment) { + messageSizeCalculators().forEach { $0.outgoingCellTopLabelAlignment = newAlignment } + } + + /// Set `incomingMessageTopLabelAlignment` of all `MessageSizeCalculator`s + public func setMessageIncomingMessageTopLabelAlignment(_ newAlignment: LabelAlignment) { + messageSizeCalculators().forEach { $0.incomingMessageTopLabelAlignment = newAlignment } + } + + /// Set `outgoingMessageTopLabelAlignment` of all `MessageSizeCalculator`s + public func setMessageOutgoingMessageTopLabelAlignment(_ newAlignment: LabelAlignment) { + messageSizeCalculators().forEach { $0.outgoingMessageTopLabelAlignment = newAlignment } + } + + /// Set `incomingMessageBottomLabelAlignment` of all `MessageSizeCalculator`s + public func setMessageIncomingMessageBottomLabelAlignment(_ newAlignment: LabelAlignment) { + messageSizeCalculators().forEach { $0.incomingMessageBottomLabelAlignment = newAlignment } + } + + /// Set `outgoingMessageBottomLabelAlignment` of all `MessageSizeCalculator`s + public func setMessageOutgoingMessageBottomLabelAlignment(_ newAlignment: LabelAlignment) { + messageSizeCalculators().forEach { $0.outgoingMessageBottomLabelAlignment = newAlignment } + } + + /// Get all `MessageSizeCalculator`s + open func messageSizeCalculators() -> [MessageSizeCalculator] { + return [textMessageSizeCalculator, attributedTextMessageSizeCalculator, emojiMessageSizeCalculator, photoMessageSizeCalculator, videoMessageSizeCalculator, locationMessageSizeCalculator] + } + }