ChatSystemMessageCell.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. @objc open class ChatSystemMessageCell: UITableViewCell {
  23. private var _systemMessage: SystemMessage?
  24. private var _msgText = UILabel.init()
  25. private var _msgBackground = UIImageView()
  26. private let _msgY: CGFloat = 12.0
  27. @objc override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  28. super.init(style: style, reuseIdentifier: reuseIdentifier)
  29. self.backgroundColor = .clear
  30. var fontSize = roundf(UserSettings.shared().chatFontSize * 13 / 16.0)
  31. if fontSize < Float(kSystemMessageMinFontSize) {
  32. fontSize = Float(kSystemMessageMinFontSize)
  33. }
  34. else if fontSize > Float(kSystemMessageMaxFontSize) {
  35. fontSize = Float(kSystemMessageMaxFontSize)
  36. }
  37. _msgBackground.clearsContextBeforeDrawing = false
  38. _msgBackground.backgroundColor = Colors.chatSystemMessageBackground()
  39. _msgBackground.autoresizingMask = .flexibleWidth
  40. _msgBackground.layer.cornerRadius = 5
  41. contentView.addSubview(_msgBackground)
  42. _msgText.frame = CGRect.init(x: 20.0, y: _msgY, width: contentView.frame.size.width - 40.0, height: 20.0)
  43. _msgText.font = UIFont.boldSystemFont(ofSize: CGFloat(roundf(fontSize)))
  44. _msgText.textColor = Colors.fontNormal()
  45. _msgText.numberOfLines = 0
  46. _msgText.textAlignment = .left
  47. _msgText.autoresizingMask = .flexibleWidth
  48. _msgText.backgroundColor = .clear
  49. contentView.addSubview(_msgText)
  50. }
  51. required public init?(coder: NSCoder) {
  52. fatalError("init(coder:) has not been implemented")
  53. }
  54. }
  55. extension ChatSystemMessageCell {
  56. // MARK: Override functions
  57. @objc open class func height(for message: BaseMessage!, forTableWidth tableWidth: CGFloat) -> CGFloat {
  58. let systemMessage = message as! SystemMessage
  59. let maxSize = CGSize.init(width: tableWidth - 40.0 - 32.0, height: CGFloat.greatestFiniteMagnitude)
  60. let text = systemMessage.format()
  61. var dummySystemLabel: UILabel? = nil
  62. if dummySystemLabel == nil {
  63. dummySystemLabel = UILabel.init(frame: CGRect.init(x: 0.0, y: 0.0, width: maxSize.width, height: maxSize.height))
  64. }
  65. var fontSize = roundf(UserSettings.shared().chatFontSize * 13 / 16.0)
  66. if fontSize < Float(kSystemMessageMinFontSize) {
  67. fontSize = Float(kSystemMessageMinFontSize)
  68. }
  69. else if fontSize > Float(kSystemMessageMaxFontSize) {
  70. fontSize = Float(kSystemMessageMaxFontSize)
  71. }
  72. dummySystemLabel!.font = UIFont.boldSystemFont(ofSize: CGFloat(roundf(fontSize)))
  73. dummySystemLabel!.numberOfLines = 3
  74. dummySystemLabel!.text = text
  75. let height = (dummySystemLabel?.sizeThatFits(maxSize).height)!
  76. return height
  77. }
  78. @objc override open func layoutSubviews() {
  79. let messageTextWidth = self.layoutMarginsGuide.layoutFrame.size.width - 32.0
  80. let textSize = _msgText.sizeThatFits(CGSize.init(width: messageTextWidth, height: CGFloat.greatestFiniteMagnitude))
  81. super.layoutSubviews()
  82. let bgSideMargin: CGFloat = 8.0
  83. let bgTopOffset: CGFloat = 6.0
  84. let bgHeightMargin: CGFloat = bgTopOffset * 2
  85. let backgroundWidth: CGFloat = bgSideMargin + textSize.width + bgSideMargin
  86. let backgroundX: CGFloat = (self.frame.size.width - backgroundWidth) / 2
  87. _msgBackground.frame = CGRect(x: backgroundX, y: _msgY - bgTopOffset, width: backgroundWidth, height: textSize.height + bgHeightMargin)
  88. _msgText.frame = CGRect.init(x: backgroundX + bgSideMargin, y: _msgY, width: textSize.width, height: textSize.height)
  89. }
  90. @objc func setMessage(systemMessage: SystemMessage) {
  91. _msgText.text = systemMessage.format()
  92. setupColors()
  93. }
  94. @objc @available(iOS 13.0, *)
  95. open func getContextMenu(_ indexPath: IndexPath!, point: CGPoint) -> UIContextMenuConfiguration! {
  96. return nil
  97. }
  98. }
  99. extension ChatSystemMessageCell {
  100. // MARK: private functions
  101. private func setupColors() {
  102. _msgText.textColor = Colors.fontNormal()
  103. _msgBackground.backgroundColor = Colors.chatSystemMessageBackground()
  104. }
  105. }