refactored textWidth into a enum

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-08-29 15:28:38 -05:00
parent 27af806281
commit 2956dc294e

View File

@ -130,37 +130,33 @@ open class Tilelet: TileContainer {
$0.name = .rightArrow $0.name = .rightArrow
} }
private var _textWidth: CGFloat? public enum TextWidth {
open var textWidth: CGFloat? { case value(CGFloat)
case percentage(CGFloat)
}
private var _textWidth: TextWidth?
/// If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered.
open var textWidth: TextWidth? {
get { _textWidth } get { _textWidth }
set { set {
if let newValue, newValue > 44.0 { if let newValue {
_textWidth = newValue switch newValue {
if _textPercentage != nil { case .percentage(let percentage):
_textPercentage = nil if percentage >= 10 && percentage <= 100.0 {
_textWidth = newValue
}
case .value(let value):
if value > 44.0 {
_textWidth = newValue
}
} }
} else { } else {
_textWidth = nil _textWidth = nil
} }
setNeedsUpdate() setNeedsUpdate()
} }
}
private var _textPercentage: CGFloat?
open var textPercentage: CGFloat? {
get { _textPercentage }
set {
if let newValue, newValue >= 5 && newValue <= 100.0 {
_textPercentage = newValue
if textWidth != nil {
_textWidth = nil
}
} else {
_textPercentage = nil
}
setNeedsUpdate()
}
} }
open var textPostion: TextPosition = .top { didSet { setNeedsUpdate() }} open var textPostion: TextPosition = .top { didSet { setNeedsUpdate() }}
@ -319,22 +315,20 @@ open class Tilelet: TileContainer {
titleLockupTrailingConstraint?.isActive = true titleLockupTrailingConstraint?.isActive = true
titleLockupWidthConstraint?.isActive = false titleLockupWidthConstraint?.isActive = false
titleLockupWidthConstraint = titleLockup.widthAnchor.constraint(equalToConstant: textWidth)
titleLockupWidthConstraint?.isActive = true
} else if let textPercentage {
titleLockupTrailingConstraint?.isActive = false
titleLockupTrailingConstraint = titleLockup.trailingAnchor.constraint(lessThanOrEqualTo: titleLockupContainerView.trailingAnchor)
titleLockupTrailingConstraint?.isActive = true
titleLockupWidthConstraint?.isActive = false switch textWidth {
titleLockupWidthConstraint = NSLayoutConstraint(item: titleLockup, case .value(let value):
attribute: .width, titleLockupWidthConstraint = titleLockup.widthAnchor.constraint(equalToConstant: value)
relatedBy: .equal, case .percentage(let percentage):
toItem: containerView, titleLockupWidthConstraint = NSLayoutConstraint(item: titleLockup,
attribute: .width, attribute: .width,
multiplier: textPercentage / 100, relatedBy: .equal,
constant: 0.0) toItem: containerView,
attribute: .width,
multiplier: percentage / 100,
constant: 0.0)
}
titleLockupWidthConstraint?.isActive = true titleLockupWidthConstraint?.isActive = true
} else { } else {