|
| 1 | +import AutoLayout |
| 2 | +import UIKit |
| 3 | + |
| 4 | +/// A UIKit component that displays a badge. |
| 5 | +open class UKBadge: UIView, UKComponent { |
| 6 | + // MARK: - Properties |
| 7 | + |
| 8 | + /// A model that defines the appearance properties. |
| 9 | + public var model: BadgeVM { |
| 10 | + didSet { |
| 11 | + self.update(oldValue) |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + private var titleLabelConstraints: LayoutConstraints = .init() |
| 16 | + |
| 17 | + // MARK: - Subviews |
| 18 | + |
| 19 | + /// A label that displays the title from the model. |
| 20 | + public var titleLabel = UILabel() |
| 21 | + |
| 22 | + // MARK: - UIView Properties |
| 23 | + |
| 24 | + open override var intrinsicContentSize: CGSize { |
| 25 | + return self.sizeThatFits(UIView.layoutFittingExpandedSize) |
| 26 | + } |
| 27 | + |
| 28 | + // MARK: - Initialization |
| 29 | + |
| 30 | + /// Initializes a new instance of `UKBadge`. |
| 31 | + /// - Parameter model: A model that defines the appearance properties for the badge. |
| 32 | + public init(model: BadgeVM = .init()) { |
| 33 | + self.model = model |
| 34 | + super.init(frame: .zero) |
| 35 | + |
| 36 | + self.setup() |
| 37 | + self.style() |
| 38 | + self.layout() |
| 39 | + } |
| 40 | + |
| 41 | + public required init?(coder: NSCoder) { |
| 42 | + fatalError("init(coder:) has not been implemented") |
| 43 | + } |
| 44 | + |
| 45 | + // MARK: - Setup |
| 46 | + |
| 47 | + private func setup() { |
| 48 | + self.addSubview(self.titleLabel) |
| 49 | + } |
| 50 | + |
| 51 | + // MARK: - Style |
| 52 | + |
| 53 | + private func style() { |
| 54 | + Self.Style.mainView(self, model: self.model) |
| 55 | + Self.Style.titleLabel(self.titleLabel, model: self.model) |
| 56 | + } |
| 57 | + |
| 58 | + // MARK: - Layout |
| 59 | + |
| 60 | + private func layout() { |
| 61 | + self.titleLabelConstraints = .merged { |
| 62 | + self.titleLabel.top(self.model.paddings.top) |
| 63 | + self.titleLabel.leading(self.model.paddings.leading) |
| 64 | + self.titleLabel.bottom(self.model.paddings.bottom) |
| 65 | + self.titleLabel.trailing(self.model.paddings.trailing) |
| 66 | + } |
| 67 | + |
| 68 | + self.titleLabelConstraints.allConstraints.forEach { $0?.priority = .defaultHigh } |
| 69 | + } |
| 70 | + |
| 71 | + open override func layoutSubviews() { |
| 72 | + super.layoutSubviews() |
| 73 | + |
| 74 | + self.layer.cornerRadius = self.model.cornerRadius.value(for: self.bounds.height) |
| 75 | + } |
| 76 | + |
| 77 | + // MARK: - Update |
| 78 | + |
| 79 | + public func update(_ oldModel: BadgeVM) { |
| 80 | + guard self.model != oldModel else { return } |
| 81 | + |
| 82 | + self.style() |
| 83 | + if self.model.shouldUpdateLayout(oldModel) { |
| 84 | + self.titleLabelConstraints.leading?.constant = self.model.paddings.leading |
| 85 | + self.titleLabelConstraints.top?.constant = self.model.paddings.top |
| 86 | + self.titleLabelConstraints.bottom?.constant = -self.model.paddings.bottom |
| 87 | + self.titleLabelConstraints.trailing?.constant = -self.model.paddings.trailing |
| 88 | + |
| 89 | + self.invalidateIntrinsicContentSize() |
| 90 | + self.setNeedsLayout() |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // MARK: - UIView Methods |
| 95 | + |
| 96 | + open override func sizeThatFits(_ size: CGSize) -> CGSize { |
| 97 | + let contentSize = self.titleLabel.sizeThatFits(size) |
| 98 | + |
| 99 | + let totalWidthPadding = self.model.paddings.leading + self.model.paddings.trailing |
| 100 | + let totalHeightPadding = self.model.paddings.top + self.model.paddings.bottom |
| 101 | + |
| 102 | + let width = contentSize.width + totalWidthPadding |
| 103 | + let height = contentSize.height + totalHeightPadding |
| 104 | + |
| 105 | + return CGSize( |
| 106 | + width: min(width, size.width), |
| 107 | + height: min(height, size.height) |
| 108 | + ) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// MARK: - Style Helpers |
| 113 | + |
| 114 | +extension UKBadge { |
| 115 | + fileprivate enum Style { |
| 116 | + static func mainView(_ view: UIView, model: BadgeVM) { |
| 117 | + view.backgroundColor = model.backgroundColor.uiColor |
| 118 | + view.layer.cornerRadius = model.cornerRadius.value(for: view.bounds.height) |
| 119 | + } |
| 120 | + static func titleLabel(_ label: UILabel, model: BadgeVM) { |
| 121 | + label.textAlignment = .center |
| 122 | + label.text = model.title |
| 123 | + label.font = model.font.uiFont |
| 124 | + label.textColor = model.foregroundColor.uiColor |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments