MessageTableViewCell.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // MessageTableViewCell.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2020/5/26.
  6. // Copyright © 2020 Fin. All rights reserved.
  7. //
  8. import Material
  9. import RxSwift
  10. import UIKit
  11. class MessageTableViewCell: BaseTableViewCell<MessageTableViewCellViewModel> {
  12. let backgroundPanel: UIView = {
  13. let view = UIView()
  14. view.layer.cornerRadius = 3
  15. view.clipsToBounds = true
  16. view.backgroundColor = BKColor.background.secondary
  17. return view
  18. }()
  19. let bodyLabel: UITextView = {
  20. let label = UITextView()
  21. label.backgroundColor = UIColor.clear
  22. label.isEditable = false
  23. label.dataDetectorTypes = [.phoneNumber, .link]
  24. label.isScrollEnabled = false
  25. label.textContainerInset = .zero
  26. label.textContainer.lineFragmentPadding = 0
  27. label.font = RobotoFont.regular(with: 14)
  28. label.textColor = BKColor.grey.darken4
  29. return label
  30. }()
  31. let dateLabel: UILabel = {
  32. let label = BKLabel()
  33. label.hitTestSlop = UIEdgeInsets(top: -5, left: -5, bottom: -5, right: -5)
  34. label.font = RobotoFont.medium(with: 11)
  35. label.textColor = BKColor.grey.base
  36. label.isUserInteractionEnabled = true
  37. label.addGestureRecognizer(UITapGestureRecognizer())
  38. return label
  39. }()
  40. let separatorLine: UIImageView = {
  41. let imageView = UIImageView()
  42. imageView.backgroundColor = BKColor.background.primary
  43. return imageView
  44. }()
  45. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  46. super.init(style: style, reuseIdentifier: reuseIdentifier)
  47. self.selectionStyle = .none
  48. self.backgroundColor = BKColor.background.primary
  49. contentView.addSubview(backgroundPanel)
  50. contentView.addSubview(bodyLabel)
  51. contentView.addSubview(dateLabel)
  52. contentView.addSubview(separatorLine)
  53. layoutView()
  54. }
  55. @available(*, unavailable)
  56. required init?(coder: NSCoder) {
  57. fatalError("init(coder:) has not been implemented")
  58. }
  59. func layoutView() {
  60. bodyLabel.snp.remakeConstraints { make in
  61. make.top.equalTo(16)
  62. make.left.equalTo(28)
  63. make.right.equalTo(-28)
  64. }
  65. dateLabel.snp.remakeConstraints { make in
  66. make.left.equalTo(bodyLabel)
  67. make.top.equalTo(bodyLabel.snp.bottom).offset(12)
  68. }
  69. separatorLine.snp.remakeConstraints { make in
  70. make.left.right.bottom.equalToSuperview()
  71. make.top.equalTo(dateLabel.snp.bottom).offset(12)
  72. make.height.equalTo(10)
  73. }
  74. backgroundPanel.snp.makeConstraints { make in
  75. make.left.equalToSuperview().offset(16)
  76. make.right.equalToSuperview().offset(-16)
  77. make.top.equalToSuperview()
  78. make.bottom.equalTo(separatorLine.snp.top)
  79. }
  80. }
  81. override func bindViewModel(model: MessageTableViewCellViewModel) {
  82. super.bindViewModel(model: model)
  83. Observable.combineLatest(model.title, model.body, model.url).subscribe { title, body, url in
  84. let text = NSMutableAttributedString(
  85. string: body,
  86. attributes: [.font: RobotoFont.regular(with: 14), .foregroundColor: BKColor.grey.darken4]
  87. )
  88. if title.count > 0 {
  89. // 插入一行空行当 spacer
  90. text.insert(NSAttributedString(
  91. string: "\n",
  92. attributes: [.font: RobotoFont.medium(with: 6)]
  93. ), at: 0)
  94. text.insert(NSAttributedString(
  95. string: title + "\n",
  96. attributes: [.font: RobotoFont.medium(with: 16), .foregroundColor: BKColor.grey.darken4]
  97. ), at: 0)
  98. }
  99. if url.count > 0 {
  100. // 插入一行空行当 spacer
  101. text.append(NSAttributedString(
  102. string: "\n ",
  103. attributes: [.font: RobotoFont.medium(with: 8)]
  104. ))
  105. text.append(NSAttributedString(string: "\n\(url)", attributes: [
  106. .font: RobotoFont.regular(with: 14),
  107. .foregroundColor: BKColor.grey.darken4,
  108. .link: url
  109. ]))
  110. }
  111. self.bodyLabel.attributedText = text
  112. }.disposed(by: rx.reuseBag)
  113. model.date.bind(to: self.dateLabel.rx.text).disposed(by: rx.reuseBag)
  114. // 切换时间显示样式
  115. dateLabel.gestureRecognizers?.first?.rx.event.subscribe(onNext: { _ in
  116. if model.dateStyle.value != .exact {
  117. model.dateStyle.accept(.exact)
  118. } else {
  119. model.dateStyle.accept(.relative)
  120. }
  121. }).disposed(by: rx.reuseBag)
  122. }
  123. }