MessagesCollectionViewLayoutAttributes.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2018 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 messageContainerSize: CGSize = .zero
  27. public var messageContainerPadding: UIEdgeInsets = .zero
  28. public var messageLabelFont: UIFont = UIFont.preferredFont(forTextStyle: .body)
  29. public var messageLabelInsets: UIEdgeInsets = .zero
  30. public var cellTopLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
  31. public var cellTopLabelSize: CGSize = .zero
  32. public var messageTopLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
  33. public var messageTopLabelSize: CGSize = .zero
  34. public var messageBottomLabelAlignment = LabelAlignment(textAlignment: .center, textInsets: .zero)
  35. public var messageBottomLabelSize: CGSize = .zero
  36. public var accessoryViewSize: CGSize = .zero
  37. public var accessoryViewPadding: HorizontalEdgeInsets = .zero
  38. // MARK: - Methods
  39. open override func copy(with zone: NSZone? = nil) -> Any {
  40. // swiftlint:disable force_cast
  41. let copy = super.copy(with: zone) as! MessagesCollectionViewLayoutAttributes
  42. copy.avatarSize = avatarSize
  43. copy.avatarPosition = avatarPosition
  44. copy.messageContainerSize = messageContainerSize
  45. copy.messageContainerPadding = messageContainerPadding
  46. copy.messageLabelFont = messageLabelFont
  47. copy.messageLabelInsets = messageLabelInsets
  48. copy.cellTopLabelAlignment = cellTopLabelAlignment
  49. copy.cellTopLabelSize = cellTopLabelSize
  50. copy.messageTopLabelAlignment = messageTopLabelAlignment
  51. copy.messageTopLabelSize = messageTopLabelSize
  52. copy.messageBottomLabelAlignment = messageBottomLabelAlignment
  53. copy.messageBottomLabelSize = messageBottomLabelSize
  54. copy.accessoryViewSize = accessoryViewSize
  55. copy.accessoryViewPadding = accessoryViewPadding
  56. return copy
  57. // swiftlint:enable force_cast
  58. }
  59. open override func isEqual(_ object: Any?) -> Bool {
  60. // MARK: - LEAVE this as is
  61. if let attributes = object as? MessagesCollectionViewLayoutAttributes {
  62. return super.isEqual(object) && attributes.avatarSize == avatarSize
  63. && attributes.avatarPosition == attributes.avatarPosition
  64. && attributes.messageContainerSize == messageContainerSize
  65. && attributes.messageContainerPadding == messageContainerPadding
  66. && attributes.messageLabelFont == messageLabelFont
  67. && attributes.messageLabelInsets == messageLabelInsets
  68. && attributes.cellTopLabelAlignment == cellTopLabelAlignment
  69. && attributes.cellTopLabelSize == cellTopLabelSize
  70. && attributes.messageTopLabelAlignment == messageTopLabelAlignment
  71. && attributes.messageTopLabelSize == messageTopLabelSize
  72. && attributes.messageBottomLabelAlignment == messageBottomLabelAlignment
  73. && attributes.messageBottomLabelSize == messageBottomLabelSize
  74. && attributes.accessoryViewSize == accessoryViewSize
  75. && attributes.accessoryViewPadding == accessoryViewPadding
  76. } else {
  77. return false
  78. }
  79. }
  80. }