LNRoomGiftMessageCell.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // LNRoomGiftMessageCell.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/26.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNRoomGiftMessageCell: UITableViewCell {
  11. private let contentLabel = UILabel()
  12. private let attachment = NSTextAttachment(image: .icGift)
  13. private var curItem: LNRoomGiftMessageItem?
  14. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  15. super.init(style: style, reuseIdentifier: reuseIdentifier)
  16. setupViews()
  17. attachment.bounds = .init(x: 0, y: -6.5, width: 22, height: 22)
  18. }
  19. func update(_ message: LNRoomGiftMessageItem) {
  20. curItem = message
  21. contentLabel.attributedText = buildContent(message)
  22. guard let iconURL = URL(string: message.giftIcon), !message.giftIcon.isEmpty else { return }
  23. SDWebImageManager.shared.loadImage(with: iconURL, progress: nil) { [weak self] image, _, error, _, _, _ in
  24. guard let self else { return }
  25. guard error == nil, let image else { return }
  26. guard self.curItem === message else { return }
  27. attachment.image = image
  28. contentLabel.attributedText = contentLabel.attributedText
  29. }
  30. }
  31. override func prepareForReuse() {
  32. super.prepareForReuse()
  33. contentLabel.attributedText = nil
  34. curItem = nil
  35. }
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. }
  40. extension LNRoomGiftMessageCell {
  41. private func buildContent(_ message: LNRoomGiftMessageItem, giftImage: UIImage = .icGift) -> NSAttributedString {
  42. let text = String(key: "B00130", message.senderName, message.receiverName, message.giftCount)
  43. let attr = NSMutableAttributedString(string: text, attributes: [
  44. .font: UIFont.heading_h5,
  45. .foregroundColor: UIColor.text_1
  46. ])
  47. let senderRange = (text as NSString).range(of: message.senderName)
  48. attr.addAttributes([
  49. .font: UIFont.heading_h5,
  50. .foregroundColor: UIColor.text_7
  51. ], range: senderRange)
  52. let receiverRange = (text as NSString).range(of: message.receiverName)
  53. attr.addAttributes([
  54. .font: UIFont.heading_h5,
  55. .foregroundColor: UIColor.text_7
  56. ], range: receiverRange)
  57. let countRange: NSRange? = if text.contains("x\(message.giftCount)") {
  58. (text as NSString).range(of: "x\(message.giftCount)")
  59. } else if text.contains("\(message.giftCount)x") {
  60. (text as NSString).range(of: "\(message.giftCount)x")
  61. } else {
  62. nil
  63. }
  64. if let countRange {
  65. attr.addAttributes([
  66. .font: UIFont.heading_h5,
  67. .foregroundColor: UIColor.text_7
  68. ], range: countRange)
  69. }
  70. let giftIconRange = (text as NSString).range(of: "{icon}")
  71. attr.replaceCharacters(in: giftIconRange, with: .init(attachment: attachment))
  72. return attr
  73. }
  74. }
  75. extension LNRoomGiftMessageCell {
  76. private func setupViews() {
  77. backgroundColor = .clear
  78. let container = UIView()
  79. container.backgroundColor = .fill.withAlphaComponent(0.12)
  80. container.layer.cornerRadius = 12
  81. container.onTap { [weak self] in
  82. guard let self else { return }
  83. guard let curItem else { return }
  84. let panel = LNRoomProfileCardPanel()
  85. panel.load(curItem.sender)
  86. panel.popup(self)
  87. }
  88. contentView.addSubview(container)
  89. container.snp.makeConstraints { make in
  90. make.verticalEdges.equalToSuperview().inset(8)
  91. make.leading.equalToSuperview()
  92. make.trailing.lessThanOrEqualToSuperview()
  93. }
  94. contentLabel.text = " "
  95. contentLabel.font = .heading_h5
  96. contentLabel.textColor = .text_1
  97. contentLabel.numberOfLines = 0
  98. container.addSubview(contentLabel)
  99. contentLabel.snp.makeConstraints { make in
  100. make.horizontalEdges.equalToSuperview().inset(10)
  101. make.verticalEdges.equalToSuperview().inset(8)
  102. }
  103. }
  104. }