ReactionsViewCell.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import UIKit
  6. @objcMembers class ReactionsViewCell: UICollectionViewCell {
  7. @IBOutlet weak var label: UILabel!
  8. override func awakeFromNib() {
  9. super.awakeFromNib()
  10. label.layer.borderWidth = 1.0
  11. label.layer.borderColor = self.borderColor()
  12. label.layer.cornerRadius = 15.0
  13. label.clipsToBounds = true
  14. label.backgroundColor = NCAppBranding.backgroundColor()
  15. }
  16. override func prepareForReuse() {
  17. super.prepareForReuse()
  18. label.text = ""
  19. label.layer.borderColor = self.borderColor()
  20. label.backgroundColor = NCAppBranding.backgroundColor()
  21. }
  22. func borderColor() -> CGColor {
  23. return UIColor.tertiaryLabel.cgColor
  24. }
  25. func sizeForReaction(reaction: NCChatReaction) -> CGSize {
  26. let text = textForReaction(reaction: reaction)
  27. var size = CGSize(width: text.width(withConstrainedHeight: 30, font: .systemFont(ofSize: 13.0)), height: 30)
  28. size.width += 20
  29. return size
  30. }
  31. func textForReaction(reaction: NCChatReaction) -> String {
  32. return reaction.reaction + " " + String(reaction.count)
  33. }
  34. func setReaction(reaction: NCChatReaction) {
  35. label.text = textForReaction(reaction: reaction)
  36. label.backgroundColor = reaction.userReacted ? NCAppBranding.elementColor().withAlphaComponent(0.15) : NCAppBranding.backgroundColor()
  37. label.layer.borderColor = reaction.userReacted ? NCAppBranding.elementColor().cgColor : self.borderColor()
  38. }
  39. }
  40. extension String {
  41. func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
  42. let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
  43. let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
  44. return ceil(boundingBox.height)
  45. }
  46. func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
  47. let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
  48. let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
  49. return ceil(boundingBox.width)
  50. }
  51. }