ActionSheetCell.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*****************************************************************************
  2. * ActionSheetCell.swift
  3. *
  4. * Copyright © 2018 VLC authors and VideoLAN
  5. * Copyright © 2018 Videolabs
  6. *
  7. * Authors: Soomin Lee <bubu@mikan.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. @objc(VLCActionSheetCell)
  12. class ActionSheetCell: UICollectionViewCell {
  13. @objc static var identifier: String {
  14. return String(describing: self)
  15. }
  16. override var isSelected: Bool {
  17. didSet {
  18. updateColors()
  19. checkmark.isHidden = !isSelected
  20. }
  21. }
  22. let icon: UIImageView = {
  23. let icon = UIImageView()
  24. icon.translatesAutoresizingMaskIntoConstraints = false
  25. icon.contentMode = .scaleAspectFit
  26. return icon
  27. }()
  28. let name: UILabel = {
  29. let name = UILabel()
  30. name.textColor = PresentationTheme.current.colors.cellTextColor
  31. name.font = UIFont.systemFont(ofSize: 15)
  32. name.translatesAutoresizingMaskIntoConstraints = false
  33. name.setContentHuggingPriority(.defaultLow, for: .horizontal)
  34. return name
  35. }()
  36. let checkmark: UILabel = {
  37. let checkmark = UILabel()
  38. checkmark.text = "✓"
  39. checkmark.font = UIFont.systemFont(ofSize: 18)
  40. checkmark.textColor = PresentationTheme.current.colors.orangeUI
  41. checkmark.translatesAutoresizingMaskIntoConstraints = false
  42. checkmark.isHidden = true
  43. return checkmark
  44. }()
  45. let stackView: UIStackView = {
  46. let stackView = UIStackView()
  47. stackView.spacing = 15.0
  48. stackView.axis = .horizontal
  49. stackView.alignment = .center
  50. stackView.translatesAutoresizingMaskIntoConstraints = false
  51. return stackView
  52. }()
  53. override init(frame: CGRect) {
  54. super.init(frame: frame)
  55. setupViews()
  56. }
  57. required init?(coder aDecoder: NSCoder) {
  58. super.init(coder: aDecoder)
  59. setupViews()
  60. }
  61. private func updateColors() {
  62. let colors = PresentationTheme.current.colors
  63. name.textColor = isSelected ? colors.orangeUI : colors.cellTextColor
  64. tintColor = isSelected ? colors.orangeUI : colors.cellDetailTextColor
  65. }
  66. override func prepareForReuse() {
  67. super.prepareForReuse()
  68. updateColors()
  69. }
  70. private func setupViews() {
  71. backgroundColor = PresentationTheme.current.colors.background
  72. stackView.addArrangedSubview(icon)
  73. stackView.addArrangedSubview(name)
  74. stackView.addArrangedSubview(checkmark)
  75. addSubview(stackView)
  76. var guide: LayoutAnchorContainer = self
  77. if #available(iOS 11.0, *) {
  78. guide = safeAreaLayoutGuide
  79. }
  80. NSLayoutConstraint.activate([
  81. icon.heightAnchor.constraint(equalToConstant: 25),
  82. icon.widthAnchor.constraint(equalTo: icon.heightAnchor),
  83. stackView.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 20),
  84. stackView.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -20),
  85. stackView.heightAnchor.constraint(equalTo: heightAnchor),
  86. stackView.topAnchor.constraint(equalTo: topAnchor)
  87. ])
  88. }
  89. }