updated badge

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-21 15:36:30 -05:00
parent c9f6caa680
commit 5502cdaf46

View File

@ -27,12 +27,11 @@ open class Badge: View {
/// Label used to render text /// Label used to render text
open var label = Label().with { 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.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.textPosition = .left
$0.textStyle = .boldBodySmall $0.textStyle = .boldBodySmall
} }
@ -40,7 +39,7 @@ open class Badge: View {
/// This will render the badges fill color based on the available options. /// 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. /// 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() }} open var fillColor: FillColor = .red { didSet { setNeedsUpdate() }}
/// The text that will be shown in the label. /// The text that will be shown in the label.
open var text: String = "" { didSet { setNeedsUpdate() }} 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. /// 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() }} open var numberOfLines: Int = 1 { didSet { setNeedsUpdate() }}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Constraints // MARK: - Constraints
//-------------------------------------------------- //--------------------------------------------------
private var maxWidthConstraint: NSLayoutConstraint? private var labelWidthConstraint: NSLayoutConstraint?
private var minWidthConstraint: 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 // MARK: - Configuration
//-------------------------------------------------- //--------------------------------------------------
@ -104,20 +113,16 @@ open class Badge: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Lifecycle // MARK: - Lifecycle
//-------------------------------------------------- //--------------------------------------------------
open override func setup() { open override func setup() {
super.setup() super.setup()
accessibilityElements = [label] accessibilityElements = [label]
layer.cornerRadius = 2 layer.cornerRadius = 2
addSubview(label) addSubview(label)
label.pinToSuperView(labelInset) label.pinToSuperView(labelInset)
maxWidthConstraint = label.widthAnchor.constraint(lessThanOrEqualToConstant: 0)
minWidthConstraint = label.widthAnchor.constraint(greaterThanOrEqualToConstant: minWidth)
minWidthConstraint?.isActive = true
} }
/// Resets to default settings. /// Resets to default settings.
@ -144,19 +149,15 @@ open class Badge: View {
backgroundColor = backgroundColorConfiguration.getColor(self) backgroundColor = backgroundColorConfiguration.getColor(self)
if let maxWidth = maxWidth, maxWidth > minWidth { labelWidthConstraint?.isActive = false
maxWidthConstraint?.constant = maxWidth labelWidthConstraint = widthConstraint
maxWidthConstraint?.isActive = true labelWidthConstraint?.isActive = true
minWidthConstraint?.isActive = false
} else {
maxWidthConstraint?.isActive = false
minWidthConstraint?.isActive = true
}
label.textColorConfiguration = textColorConfiguration.eraseToAnyColorable() label.textColorConfiguration = textColorConfiguration.eraseToAnyColorable()
label.numberOfLines = numberOfLines label.numberOfLines = numberOfLines
label.text = text label.text = text
label.surface = surface label.surface = surface
label.disabled = disabled label.disabled = disabled
} }
} }