MessagesCollectionViewLayoutAttributes.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2019 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import UIKit
  21. /// The layout attributes used by a `MessageCollectionViewCell` to layout its subviews.
  22. open class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttributes {
  23. // MARK: - Properties
  24. public var avatarSize: CGSize = .zero
  25. public var avatarPosition = AvatarPosition(vertical: .cellBottom)
  26. public var avatarLeadingTrailingPadding: CGFloat = 0
  27. public var messageContainerSize: CGSize = .zero
  28. public var messageContainerPadding: UIEdgeInsets = .zero
  29. public var messageLabelFont: UIFont = UIFont.preferredFont(forTextStyle: .body)
  30. public var messageLabelInsets: UIEdgeInsets = .zero
  31. public var cellTopLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
  32. public var cellTopLabelSize: CGSize = .zero
  33. public var cellBottomLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
  34. public var cellBottomLabelSize: CGSize = .zero
  35. public var messageTopLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
  36. public var messageTopLabelSize: CGSize = .zero
  37. public var messageBottomLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
  38. public var messageBottomLabelSize: CGSize = .zero
  39. public var accessoryViewSize: CGSize = .zero
  40. public var accessoryViewPadding: HorizontalEdgeInsets = .zero
  41. public var accessoryViewPosition: AccessoryPosition = .messageCenter
  42. // MARK: - Methods
  43. open override func copy(with zone: NSZone? = nil) -> Any {
  44. // swiftlint:disable force_cast
  45. let copy = super.copy(with: zone) as! MessagesCollectionViewLayoutAttributes
  46. copy.avatarSize = avatarSize
  47. copy.avatarPosition = avatarPosition
  48. copy.avatarLeadingTrailingPadding = avatarLeadingTrailingPadding
  49. copy.messageContainerSize = messageContainerSize
  50. copy.messageContainerPadding = messageContainerPadding
  51. copy.messageLabelFont = messageLabelFont
  52. copy.messageLabelInsets = messageLabelInsets
  53. copy.cellTopLabelAlignment = cellTopLabelAlignment
  54. copy.cellTopLabelSize = cellTopLabelSize
  55. copy.cellBottomLabelAlignment = cellBottomLabelAlignment
  56. copy.cellBottomLabelSize = cellBottomLabelSize
  57. copy.messageTopLabelAlignment = messageTopLabelAlignment
  58. copy.messageTopLabelSize = messageTopLabelSize
  59. copy.messageBottomLabelAlignment = messageBottomLabelAlignment
  60. copy.messageBottomLabelSize = messageBottomLabelSize
  61. copy.accessoryViewSize = accessoryViewSize
  62. copy.accessoryViewPadding = accessoryViewPadding
  63. copy.accessoryViewPosition = accessoryViewPosition
  64. return copy
  65. // swiftlint:enable force_cast
  66. }
  67. open override func isEqual(_ object: Any?) -> Bool {
  68. // MARK: - LEAVE this as is
  69. if let attributes = object as? MessagesCollectionViewLayoutAttributes {
  70. return super.isEqual(object) && attributes.avatarSize == avatarSize
  71. && attributes.avatarPosition == avatarPosition
  72. && attributes.avatarLeadingTrailingPadding == avatarLeadingTrailingPadding
  73. && attributes.messageContainerSize == messageContainerSize
  74. && attributes.messageContainerPadding == messageContainerPadding
  75. && attributes.messageLabelFont == messageLabelFont
  76. && attributes.messageLabelInsets == messageLabelInsets
  77. && attributes.cellTopLabelAlignment == cellTopLabelAlignment
  78. && attributes.cellTopLabelSize == cellTopLabelSize
  79. && attributes.cellBottomLabelAlignment == cellBottomLabelAlignment
  80. && attributes.cellBottomLabelSize == cellBottomLabelSize
  81. && attributes.messageTopLabelAlignment == messageTopLabelAlignment
  82. && attributes.messageTopLabelSize == messageTopLabelSize
  83. && attributes.messageBottomLabelAlignment == messageBottomLabelAlignment
  84. && attributes.messageBottomLabelSize == messageBottomLabelSize
  85. && attributes.accessoryViewSize == accessoryViewSize
  86. && attributes.accessoryViewPadding == accessoryViewPadding
  87. && attributes.accessoryViewPosition == accessoryViewPosition
  88. } else {
  89. return false
  90. }
  91. }
  92. }