diff --git a/VDS/Components/Badge/Badge.swift b/VDS/Components/Badge/Badge.swift index 70111855..33428b8d 100644 --- a/VDS/Components/Badge/Badge.swift +++ b/VDS/Components/Badge/Badge.swift @@ -27,12 +27,11 @@ open class Badge: View { /// Label used to render text open var label = Label().with { - $0.setContentHuggingPriority(.required, for: .vertical) - $0.setContentCompressionResistancePriority(.required, for: .vertical) - $0.setContentHuggingPriority(.required, for: .horizontal) - $0.setContentCompressionResistancePriority(.required, for: .horizontal) - $0.adjustsFontSizeToFitWidth = false $0.lineBreakMode = .byTruncatingTail + $0.setContentCompressionResistancePriority(.required, for: .vertical) + $0.setContentHuggingPriority(.defaultHigh, for: .vertical) + $0.setContentCompressionResistancePriority(.required, for: .horizontal) + $0.setContentHuggingPriority(.defaultHigh, for: .horizontal) $0.textPosition = .left $0.textStyle = .boldBodySmall } @@ -40,7 +39,7 @@ open class Badge: View { /// This will render the badges fill color based on the available options. /// When used in conjunction with the surface prop, this fill color will change its tint automatically based on a light or dark surface. open var fillColor: FillColor = .red { didSet { setNeedsUpdate() }} - + /// The text that will be shown in the label. open var text: String = "" { didSet { setNeedsUpdate() }} @@ -49,13 +48,23 @@ open class Badge: View { /// This will restrict the badge height to a specific number of lines. If the text overflows the allowable space, ellipsis will show. open var numberOfLines: Int = 1 { didSet { setNeedsUpdate() }} - + //-------------------------------------------------- // MARK: - Constraints //-------------------------------------------------- - private var maxWidthConstraint: NSLayoutConstraint? - private var minWidthConstraint: NSLayoutConstraint? - + private var labelWidthConstraint: NSLayoutConstraint? + + private var widthConstraint: NSLayoutConstraint { + // Determine which constraint to activate based on the maxWidth and minWidth properties + if let maxWidth = maxWidth, maxWidth > minWidth { + // Apply maximum width constraint if set and greater than minWidth + return label.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth) + } else { + // Apply minimum width constraint + return label.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth) + } + } + //-------------------------------------------------- // MARK: - Configuration //-------------------------------------------------- @@ -104,20 +113,16 @@ open class Badge: View { //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- - + open override func setup() { super.setup() accessibilityElements = [label] layer.cornerRadius = 2 - + addSubview(label) label.pinToSuperView(labelInset) - maxWidthConstraint = label.widthAnchor.constraint(lessThanOrEqualToConstant: 0) - minWidthConstraint = label.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth) - minWidthConstraint?.isActive = true - } /// Resets to default settings. @@ -144,19 +149,15 @@ open class Badge: View { backgroundColor = backgroundColorConfiguration.getColor(self) - if let maxWidth = maxWidth, maxWidth > minWidth { - maxWidthConstraint?.constant = maxWidth - maxWidthConstraint?.isActive = true - minWidthConstraint?.isActive = false - } else { - maxWidthConstraint?.isActive = false - minWidthConstraint?.isActive = true - } + labelWidthConstraint?.isActive = false + labelWidthConstraint = widthConstraint + labelWidthConstraint?.isActive = true label.textColorConfiguration = textColorConfiguration.eraseToAnyColorable() label.numberOfLines = numberOfLines label.text = text label.surface = surface label.disabled = disabled + } }