ChatTitleView.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import UIKit
  2. class ChatTitleView: UIView {
  3. private var titleLabel: UILabel = {
  4. let titleLabel = UILabel()
  5. titleLabel.backgroundColor = UIColor.clear
  6. titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
  7. titleLabel.textAlignment = .center
  8. titleLabel.adjustsFontSizeToFitWidth = true
  9. return titleLabel
  10. }()
  11. private var subtitleLabel: UILabel = {
  12. let subtitleLabel = UILabel()
  13. subtitleLabel.font = UIFont.systemFont(ofSize: 12)
  14. subtitleLabel.textAlignment = .center
  15. return subtitleLabel
  16. }()
  17. init() {
  18. super.init(frame: .zero)
  19. setupSubviews()
  20. isAccessibilityElement = true
  21. }
  22. required init?(coder aDecoder: NSCoder) {
  23. fatalError("init(coder:) has not been implemented")
  24. }
  25. private func setupSubviews() {
  26. addSubview(titleLabel)
  27. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  28. titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  29. titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
  30. titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
  31. titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
  32. addSubview(subtitleLabel)
  33. subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
  34. subtitleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0).isActive = true
  35. subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 0).isActive = true
  36. subtitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0).isActive = true
  37. subtitleLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
  38. }
  39. func updateTitleView(title: String, subtitle: String?, baseColor: UIColor = DcColors.defaultTextColor) {
  40. subtitleLabel.textColor = baseColor.withAlphaComponent(0.95)
  41. titleLabel.textColor = baseColor
  42. titleLabel.text = title
  43. subtitleLabel.text = subtitle
  44. }
  45. }