InfoMessageCell.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import UIKit
  2. import DcCore
  3. open class InfoMessageCell: UICollectionViewCell {
  4. let label = MessageLabel()
  5. public override init(frame: CGRect) {
  6. super.init(frame: frame)
  7. contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  8. setupSubviews()
  9. }
  10. public required init?(coder aDecoder: NSCoder) {
  11. super.init(coder: aDecoder)
  12. contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  13. setupSubviews()
  14. }
  15. open override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
  16. super.apply(layoutAttributes)
  17. if let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes {
  18. label.textInsets = attributes.messageLabelInsets
  19. label.messageLabelFont = attributes.messageLabelFont
  20. label.backgroundColor = DcColors.systemMessageBackgroundColor
  21. let padding = (contentView.frame.width - attributes.messageContainerSize.width) / 2
  22. label.frame = CGRect(origin: CGPoint(x: padding, y: .zero), size: attributes.messageContainerSize)
  23. }
  24. }
  25. open override func prepareForReuse() {
  26. super.prepareForReuse()
  27. label.attributedText = nil
  28. label.text = nil
  29. }
  30. open func setupSubviews() {
  31. contentView.addSubview(label)
  32. label.layer.cornerRadius = 16
  33. label.layer.masksToBounds = true
  34. label.textAlignment = .center
  35. }
  36. open func configure(with message: MessageType, at indexPath: IndexPath, and collectionView: MessagesCollectionView) {
  37. label.configure {
  38. switch message.kind {
  39. case let .info(data):
  40. label.attributedText = data
  41. default:
  42. break
  43. }
  44. }
  45. }
  46. }