ChatTitleView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import UIKit
  2. class ChatTitleView: UIView {
  3. private var titleLabel: UILabel = {
  4. let titleLabel = UILabel()
  5. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  6. titleLabel.backgroundColor = UIColor.clear
  7. titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
  8. titleLabel.textAlignment = .center
  9. titleLabel.adjustsFontSizeToFitWidth = true
  10. return titleLabel
  11. }()
  12. private var subtitleLabel: UILabel = {
  13. let subtitleLabel = UILabel()
  14. subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
  15. subtitleLabel.font = UIFont.systemFont(ofSize: 12)
  16. subtitleLabel.textAlignment = .center
  17. return subtitleLabel
  18. }()
  19. private let locationStreamingIndicator: UIImageView = {
  20. let view = UIImageView()
  21. view.tintColor = DcColors.checkmarkGreen
  22. view.translatesAutoresizingMaskIntoConstraints = false
  23. view.constraintHeightTo(28).isActive = true
  24. view.constraintWidthTo(28).isActive = true
  25. view.image = #imageLiteral(resourceName: "ic_location").withRenderingMode(.alwaysTemplate)
  26. view.isHidden = true
  27. return view
  28. }()
  29. private let paddingNaviationButtons = 120
  30. private let sizeStreamingIndicator = 28
  31. init() {
  32. super.init(frame: .zero)
  33. setupSubviews()
  34. isAccessibilityElement = true
  35. }
  36. required init?(coder aDecoder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. private func setupSubviews() {
  40. let containerView = UIView()
  41. containerView.translatesAutoresizingMaskIntoConstraints = false
  42. addSubview(containerView)
  43. addConstraints([ containerView.constraintAlignTopTo(self),
  44. containerView.constraintAlignBottomTo(self),
  45. containerView.constraintCenterXTo(self),
  46. containerView.constraintWidthTo(UIScreen.main.bounds.width - CGFloat(paddingNaviationButtons + sizeStreamingIndicator)) ])
  47. containerView.addSubview(titleLabel)
  48. containerView.addConstraints([ titleLabel.constraintAlignLeadingTo(containerView),
  49. titleLabel.constraintAlignTrailingTo(containerView),
  50. titleLabel.constraintAlignTopTo(containerView) ])
  51. containerView.addSubview(subtitleLabel)
  52. containerView.addConstraints([ subtitleLabel.constraintToBottomOf(titleLabel),
  53. subtitleLabel.constraintAlignLeadingTo(containerView),
  54. subtitleLabel.constraintAlignTrailingTo(containerView),
  55. subtitleLabel.constraintAlignBottomTo(containerView)])
  56. addSubview(locationStreamingIndicator)
  57. addConstraints([
  58. locationStreamingIndicator.constraintCenterYTo(self),
  59. locationStreamingIndicator.constraintAlignTrailingTo(self),
  60. locationStreamingIndicator.constraintToTrailingOf(containerView)])
  61. }
  62. func updateTitleView(title: String, subtitle: String?, baseColor: UIColor = DcColors.defaultTextColor, isLocationStreaming: Bool) {
  63. subtitleLabel.textColor = baseColor.withAlphaComponent(0.95)
  64. titleLabel.textColor = baseColor
  65. titleLabel.text = title
  66. subtitleLabel.text = subtitle
  67. locationStreamingIndicator.isHidden = !isLocationStreaming
  68. }
  69. func hideLocationStreamingIndicator() {
  70. locationStreamingIndicator.isHidden = true
  71. }
  72. }