refactored the 3 buttons to use basebutton
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
372dd0cfd0
commit
1a45cfa6c1
@ -11,26 +11,14 @@ import VDSColorTokens
|
|||||||
import VDSFormControlsTokens
|
import VDSFormControlsTokens
|
||||||
import Combine
|
import Combine
|
||||||
|
|
||||||
public protocol Buttonable: UIControl, Surfaceable, Disabling {
|
|
||||||
var availableSizes: [ButtonSize] { get }
|
|
||||||
var text: String? { get set }
|
|
||||||
var intrinsicContentSize: CGSize { get }
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum ButtonSize: String, Codable, CaseIterable {
|
public enum ButtonSize: String, Codable, CaseIterable {
|
||||||
case large
|
case large
|
||||||
case small
|
case small
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc(VDSButton)
|
@objc(VDSButton)
|
||||||
open class Button: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable, Useable {
|
open class Button: BaseButton, Useable {
|
||||||
|
|
||||||
//--------------------------------------------------
|
|
||||||
// MARK: - Combine Properties
|
|
||||||
//--------------------------------------------------
|
|
||||||
public var subject = PassthroughSubject<Void, Never>()
|
|
||||||
public var subscribers = Set<AnyCancellable>()
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -42,31 +30,21 @@ open class Button: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable,
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public var availableSizes: [ButtonSize] { [.large, .small] }
|
open override var availableSizes: [ButtonSize] { [.large, .small] }
|
||||||
|
|
||||||
open var text: String? { didSet { didChange() } }
|
|
||||||
|
|
||||||
open var use: Use = .primary { didSet { didChange() }}
|
open var use: Use = .primary { didSet { didChange() }}
|
||||||
|
|
||||||
open var size: ButtonSize = .large { didSet { didChange() }}
|
open var size: ButtonSize = .large { didSet { didChange() }}
|
||||||
|
|
||||||
open var width: CGFloat? { didSet { didChange() }}
|
open var width: CGFloat? { didSet { didChange() }}
|
||||||
|
|
||||||
open var surface: Surface = .light { didSet { didChange() }}
|
|
||||||
|
|
||||||
open var disabled: Bool = false { didSet { isEnabled = !disabled } }
|
|
||||||
|
|
||||||
open override var isEnabled: Bool {
|
open override var textColor: UIColor {
|
||||||
get { !disabled }
|
buttonTitleColorConfiguration.getColor(self)
|
||||||
set {
|
}
|
||||||
if disabled != !newValue {
|
|
||||||
disabled = !newValue
|
open override var typograpicalStyle: TypographicalStyle {
|
||||||
}
|
size == .large ? TypographicalStyle.BoldBodyLarge : TypographicalStyle.BoldBodySmall
|
||||||
isUserInteractionEnabled = isEnabled
|
|
||||||
didChange()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Configuration Properties
|
// MARK: - Configuration Properties
|
||||||
@ -76,23 +54,35 @@ open class Button: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable,
|
|||||||
$0.primary.enabled.darkColor = VDSColor.backgroundPrimaryLight
|
$0.primary.enabled.darkColor = VDSColor.backgroundPrimaryLight
|
||||||
$0.primary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
$0.primary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
||||||
$0.primary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
$0.primary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
||||||
|
|
||||||
|
$0.primaryHighlighted.lightColor = VDSColor.interactiveActiveOnlight
|
||||||
|
$0.primaryHighlighted.darkColor = VDSColor.interactiveActiveOndark
|
||||||
|
|
||||||
$0.secondary.enabled.lightColor = UIColor.clear
|
$0.secondary.enabled.lightColor = UIColor.clear
|
||||||
$0.secondary.enabled.darkColor = UIColor.clear
|
$0.secondary.enabled.darkColor = UIColor.clear
|
||||||
$0.secondary.disabled.lightColor = UIColor.clear
|
$0.secondary.disabled.lightColor = UIColor.clear
|
||||||
$0.secondary.disabled.darkColor = UIColor.clear
|
$0.secondary.disabled.darkColor = UIColor.clear
|
||||||
|
|
||||||
|
$0.secondaryHighlighted.lightColor = UIColor.clear
|
||||||
|
$0.secondaryHighlighted.darkColor = UIColor.clear
|
||||||
}
|
}
|
||||||
|
|
||||||
private var buttonBorderColorConfiguration = UseableColorConfiguration().with {
|
private var buttonBorderColorConfiguration = UseableColorConfiguration().with {
|
||||||
$0.primary.enabled.lightColor = VDSColor.elementsPrimaryOndark
|
$0.primary.enabled.lightColor = VDSColor.elementsPrimaryOndark
|
||||||
$0.primary.enabled.darkColor = VDSColor.elementsPrimaryOnlight
|
$0.primary.enabled.darkColor = VDSColor.elementsPrimaryOnlight
|
||||||
$0.primary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
$0.primary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
||||||
$0.primary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
$0.primary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
||||||
|
|
||||||
|
$0.primaryHighlighted.lightColor = VDSColor.elementsPrimaryOndark
|
||||||
|
$0.primaryHighlighted.darkColor = VDSColor.elementsPrimaryOnlight
|
||||||
|
|
||||||
$0.secondary.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
$0.secondary.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
||||||
$0.secondary.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
$0.secondary.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
||||||
$0.secondary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
$0.secondary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
||||||
$0.secondary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
$0.secondary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
||||||
|
|
||||||
|
$0.secondaryHighlighted.lightColor = VDSColor.interactiveActiveOnlight
|
||||||
|
$0.secondaryHighlighted.darkColor = VDSColor.interactiveActiveOndark
|
||||||
}
|
}
|
||||||
|
|
||||||
private var buttonTitleColorConfiguration = UseableColorConfiguration().with {
|
private var buttonTitleColorConfiguration = UseableColorConfiguration().with {
|
||||||
@ -100,11 +90,17 @@ open class Button: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable,
|
|||||||
$0.primary.enabled.darkColor = VDSColor.elementsPrimaryOnlight
|
$0.primary.enabled.darkColor = VDSColor.elementsPrimaryOnlight
|
||||||
$0.primary.disabled.lightColor = VDSColor.elementsPrimaryOndark
|
$0.primary.disabled.lightColor = VDSColor.elementsPrimaryOndark
|
||||||
$0.primary.disabled.darkColor = VDSColor.elementsPrimaryOnlight
|
$0.primary.disabled.darkColor = VDSColor.elementsPrimaryOnlight
|
||||||
|
|
||||||
|
$0.primaryHighlighted.lightColor = VDSColor.elementsPrimaryOndark
|
||||||
|
$0.primaryHighlighted.darkColor = VDSColor.elementsPrimaryOnlight
|
||||||
|
|
||||||
$0.secondary.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
$0.secondary.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
||||||
$0.secondary.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
$0.secondary.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
||||||
$0.secondary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
$0.secondary.disabled.lightColor = VDSColor.interactiveDisabledOnlight
|
||||||
$0.secondary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
$0.secondary.disabled.darkColor = VDSColor.interactiveDisabledOndark
|
||||||
|
|
||||||
|
$0.secondaryHighlighted.lightColor = VDSColor.interactiveActiveOnlight
|
||||||
|
$0.secondaryHighlighted.darkColor = VDSColor.interactiveActiveOndark
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -112,91 +108,57 @@ open class Button: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable,
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
required public init() {
|
required public init() {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override init(frame: CGRect) {
|
public override init(frame: CGRect) {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public required init?(coder: NSCoder) {
|
public required init?(coder: NSCoder) {
|
||||||
super.init(coder: coder)
|
super.init(coder: coder)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Public Functions
|
// MARK: - Public Functions
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open func initialSetup() {
|
|
||||||
if !initialSetupPerformed {
|
|
||||||
backgroundColor = .clear
|
|
||||||
translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
accessibilityCustomActions = []
|
|
||||||
accessibilityTraits = .staticText
|
|
||||||
setup()
|
|
||||||
setupDidChangeEvent()
|
|
||||||
updateView()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
open func setup() {
|
open override func setup() {
|
||||||
translatesAutoresizingMaskIntoConstraints = false
|
super.setup()
|
||||||
titleLabel?.adjustsFontSizeToFitWidth = false
|
|
||||||
titleLabel?.lineBreakMode = .byTruncatingTail
|
|
||||||
|
|
||||||
//only 1 of the 2 widths can be on at the same time
|
//only 1 of the 2 widths can be on at the same time
|
||||||
widthConstraint = widthAnchor.constraint(equalToConstant: 0)
|
widthConstraint = widthAnchor.constraint(equalToConstant: 0)
|
||||||
minWidthConstraint = widthAnchor.constraint(greaterThanOrEqualToConstant: size.minimumWidth)
|
minWidthConstraint = widthAnchor.constraint(greaterThanOrEqualToConstant: size.minimumWidth)
|
||||||
|
|
||||||
//height
|
//height
|
||||||
heightConstraint = heightAnchor.constraint(equalToConstant: size.height)
|
heightConstraint = heightAnchor.constraint(equalToConstant: 0)
|
||||||
heightConstraint?.isActive = true
|
heightConstraint?.isActive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
open func reset() {
|
open override func reset() {
|
||||||
surface = .light
|
super.reset()
|
||||||
disabled = false
|
|
||||||
use = .primary
|
use = .primary
|
||||||
width = nil
|
width = nil
|
||||||
size = .large
|
size = .large
|
||||||
accessibilityCustomActions = []
|
|
||||||
accessibilityTraits = .staticText
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
override open var intrinsicContentSize: CGSize {
|
open override func updateView() {
|
||||||
let intrinsicContentSize = super.intrinsicContentSize
|
super.updateView()
|
||||||
|
|
||||||
let adjustedWidth = intrinsicContentSize.width + titleEdgeInsets.left + titleEdgeInsets.right
|
|
||||||
let adjustedHeight = intrinsicContentSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom
|
|
||||||
|
|
||||||
return CGSize(width: adjustedWidth, height: adjustedHeight)
|
|
||||||
}
|
|
||||||
|
|
||||||
open func updateView() {
|
|
||||||
|
|
||||||
let bgColor = buttonBackgroundColorConfiguration.getColor(self)
|
let bgColor = buttonBackgroundColorConfiguration.getColor(self)
|
||||||
let borderColor = buttonBorderColorConfiguration.getColor(self)
|
let borderColor = buttonBorderColorConfiguration.getColor(self)
|
||||||
let titleColor = buttonTitleColorConfiguration.getColor(self)
|
|
||||||
let borderWidth = use == .secondary ? 1.0 : 0.0
|
let borderWidth = use == .secondary ? 1.0 : 0.0
|
||||||
let buttonHeight = size.height
|
let buttonHeight = size.height
|
||||||
let cornerRadius = buttonHeight / 2
|
let cornerRadius = size.cornerRadius
|
||||||
let minWidth = size.minimumWidth
|
let minWidth = size.minimumWidth
|
||||||
let font = size == .large ? TypographicalStyle.BoldBodyLarge.font : TypographicalStyle.BoldBodySmall.font
|
|
||||||
let edgeInsets = size.edgeInsets
|
let edgeInsets = size.edgeInsets
|
||||||
|
|
||||||
setTitle(text ?? "No Text", for: .normal)
|
|
||||||
titleLabel?.font = font
|
|
||||||
backgroundColor = bgColor
|
backgroundColor = bgColor
|
||||||
setTitleColor(titleColor, for: .normal)
|
|
||||||
layer.borderColor = borderColor.cgColor
|
layer.borderColor = borderColor.cgColor
|
||||||
layer.cornerRadius = cornerRadius
|
layer.cornerRadius = cornerRadius
|
||||||
layer.borderWidth = borderWidth
|
layer.borderWidth = borderWidth
|
||||||
contentEdgeInsets = edgeInsets
|
contentEdgeInsets = edgeInsets
|
||||||
|
|
||||||
minWidthConstraint?.constant = minWidth
|
minWidthConstraint?.constant = minWidth
|
||||||
heightConstraint?.constant = buttonHeight
|
heightConstraint?.constant = buttonHeight
|
||||||
|
|
||||||
@ -215,17 +177,23 @@ open class Button: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable,
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
private class UseableColorConfiguration: ObjectColorable {
|
private class UseableColorConfiguration: ObjectColorable {
|
||||||
typealias ObjectType = Disabling & Surfaceable & Useable
|
typealias ObjectType = Buttonable & Useable
|
||||||
public var primary = DisabledSurfaceColorConfiguration()
|
public var primary = DisabledSurfaceColorConfiguration()
|
||||||
public var secondary = DisabledSurfaceColorConfiguration()
|
public var secondary = DisabledSurfaceColorConfiguration()
|
||||||
|
|
||||||
|
public var primaryHighlighted = SurfaceColorConfiguration()
|
||||||
|
public var secondaryHighlighted = SurfaceColorConfiguration()
|
||||||
|
|
||||||
required public init(){}
|
required public init(){}
|
||||||
|
|
||||||
public func getColor(_ object: ObjectType) -> UIColor {
|
public func getColor(_ object: ObjectType) -> UIColor {
|
||||||
return object.use == .primary ? primary.getColor(object) : secondary.getColor(object)
|
if object.isHighlighted {
|
||||||
|
return object.use == .primary ? primaryHighlighted.getColor(object) : secondaryHighlighted.getColor(object)
|
||||||
|
} else {
|
||||||
|
return object.use == .primary ? primary.getColor(object) : secondary.getColor(object)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ButtonSize {
|
extension ButtonSize {
|
||||||
@ -239,6 +207,10 @@ extension ButtonSize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public var cornerRadius: CGFloat {
|
||||||
|
height / 2
|
||||||
|
}
|
||||||
|
|
||||||
public var minimumWidth: CGFloat {
|
public var minimumWidth: CGFloat {
|
||||||
switch self {
|
switch self {
|
||||||
case .large:
|
case .large:
|
||||||
@ -267,7 +239,6 @@ extension ButtonSize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension Use {
|
extension Use {
|
||||||
|
|
||||||
public var color: UIColor {
|
public var color: UIColor {
|
||||||
return self == .primary ? VDSColor.backgroundPrimaryDark : .clear
|
return self == .primary ? VDSColor.backgroundPrimaryDark : .clear
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,23 +12,40 @@ import VDSFormControlsTokens
|
|||||||
import Combine
|
import Combine
|
||||||
|
|
||||||
@objc(VDSTextLink)
|
@objc(VDSTextLink)
|
||||||
open class TextLink: Control, Buttonable {
|
open class TextLink: BaseButton {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var heightConstraint: NSLayoutConstraint?
|
private var heightConstraint: NSLayoutConstraint?
|
||||||
private var label = Label()
|
private var lineHeightConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open var text: String? { didSet { didChange() } }
|
|
||||||
|
|
||||||
open var size: ButtonSize = .large { didSet { didChange() }}
|
open var size: ButtonSize = .large { didSet { didChange() }}
|
||||||
|
|
||||||
public var availableSizes: [ButtonSize] { [.large, .small] }
|
open override var availableSizes: [ButtonSize] { [.large, .small] }
|
||||||
|
|
||||||
|
open override var typograpicalStyle: TypographicalStyle {
|
||||||
|
size == .large ? TypographicalStyle.BodyLarge : TypographicalStyle.BodySmall
|
||||||
|
}
|
||||||
|
|
||||||
|
open override var textColor: UIColor {
|
||||||
|
textColorConfiguration.getColor(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var textColorConfiguration = HighlightDisabledSurfaceColorConfiguration().with {
|
||||||
|
$0.disabled.lightColor = VDSColor.elementsSecondaryOnlight
|
||||||
|
$0.disabled.darkColor = VDSColor.elementsSecondaryOndark
|
||||||
|
$0.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
||||||
|
$0.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
||||||
|
|
||||||
|
$0.highlighted.lightColor = VDSColor.interactiveActiveOnlight
|
||||||
|
$0.highlighted.darkColor = VDSColor.interactiveActiveOndark
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private var height: CGFloat {
|
private var height: CGFloat {
|
||||||
switch size {
|
switch size {
|
||||||
case .large:
|
case .large:
|
||||||
@ -43,49 +60,42 @@ open class TextLink: Control, Buttonable {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
required public init() {
|
required public init() {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override init(frame: CGRect) {
|
public override init(frame: CGRect) {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public required init?(coder: NSCoder) {
|
public required init?(coder: NSCoder) {
|
||||||
super.init(coder: coder)
|
super.init(coder: coder)
|
||||||
initialSetup()
|
}
|
||||||
|
|
||||||
|
private var line = UIView().with {
|
||||||
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Public Functions
|
// MARK: - Public Functions
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open override func initialSetup() {
|
|
||||||
super.initialSetup()
|
|
||||||
}
|
|
||||||
|
|
||||||
open override func setup() {
|
open override func setup() {
|
||||||
super.setup()
|
super.setup()
|
||||||
|
|
||||||
addSubview(label)
|
if let titleLabel {
|
||||||
|
addSubview(line)
|
||||||
//add tapGesture to self
|
line.pinLeading(titleLabel.leadingAnchor)
|
||||||
publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in
|
line.pinTrailing(titleLabel.trailingAnchor)
|
||||||
self?.sendActions(for: .touchUpInside)
|
line.pinTop(titleLabel.bottomAnchor, 1)
|
||||||
}.store(in: &subscribers)
|
lineHeightConstraint = line.heightAnchor.constraint(equalToConstant: 1.0)
|
||||||
|
lineHeightConstraint?.isActive = true
|
||||||
//pin stackview to edges
|
}
|
||||||
label.pinToSuperView()
|
|
||||||
label.numberOfLines = 1
|
|
||||||
heightConstraint = heightAnchor.constraint(equalToConstant: height)
|
heightConstraint = heightAnchor.constraint(equalToConstant: height)
|
||||||
heightConstraint?.isActive = true
|
heightConstraint?.isActive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
open override func reset() {
|
open override func reset() {
|
||||||
super.reset()
|
super.reset()
|
||||||
label.reset()
|
size = .large
|
||||||
size = .large
|
|
||||||
text = nil
|
|
||||||
|
|
||||||
accessibilityCustomActions = []
|
accessibilityCustomActions = []
|
||||||
accessibilityTraits = .staticText
|
accessibilityTraits = .staticText
|
||||||
}
|
}
|
||||||
@ -93,17 +103,32 @@ open class TextLink: Control, Buttonable {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
override open var intrinsicContentSize: CGSize {
|
|
||||||
return CGSize(width: label.intrinsicContentSize.width, height: height)
|
|
||||||
}
|
|
||||||
|
|
||||||
open override func updateView() {
|
open override func updateView() {
|
||||||
label.surface = surface
|
//need to set the properties so the super class
|
||||||
label.disabled = disabled
|
//can render out the label correctly
|
||||||
label.typograpicalStyle = size == .large ? TypographicalStyle.BodyLarge : TypographicalStyle.BodySmall
|
|
||||||
label.text = text ?? ""
|
|
||||||
label.attributes = [UnderlineLabelAttribute(location: 0, length: label.text!.count)]
|
|
||||||
heightConstraint?.constant = height
|
heightConstraint?.constant = height
|
||||||
|
lineHeightConstraint?.constant = isHighlighted ? 2.0 : 1.0
|
||||||
|
line.backgroundColor = textColor
|
||||||
|
|
||||||
|
//always call last so the label is rendered
|
||||||
|
super.updateView()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class HighlightDisabledSurfaceColorConfiguration: ObjectColorable {
|
||||||
|
typealias ObjectType = Buttonable
|
||||||
|
public var highlighted = SurfaceColorConfiguration()
|
||||||
|
public var disabled = SurfaceColorConfiguration()
|
||||||
|
public var enabled = SurfaceColorConfiguration()
|
||||||
|
|
||||||
|
required public init(){}
|
||||||
|
|
||||||
|
public func getColor(_ object: any ObjectType) -> UIColor {
|
||||||
|
if object.isHighlighted {
|
||||||
|
return highlighted.getColor(object)
|
||||||
|
} else {
|
||||||
|
return object.disabled ? disabled.getColor(object) : enabled.getColor(object)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -16,103 +16,103 @@ public enum TextLinkCaretPosition: String, CaseIterable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc(VDSTextLinkCaret)
|
@objc(VDSTextLinkCaret)
|
||||||
open class TextLinkCaret: Control, Buttonable {
|
open class TextLinkCaret: BaseButton {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var heightConstraint: NSLayoutConstraint?
|
private var heightConstraint: NSLayoutConstraint?
|
||||||
|
|
||||||
private var label = Label().with {
|
open override var typograpicalStyle: TypographicalStyle {
|
||||||
$0.typograpicalStyle = TypographicalStyle.BoldBodyLarge
|
TypographicalStyle.BoldBodyLarge
|
||||||
}
|
}
|
||||||
|
|
||||||
private var caretView = CaretView().with {
|
private var caretView = CaretView().with {
|
||||||
$0.size = CaretView.CaretSize.small(.vertical)
|
$0.size = CaretView.CaretSize.small(.vertical)
|
||||||
$0.lineWidth = 2
|
$0.lineWidth = 2
|
||||||
}
|
}
|
||||||
|
private var imageAttribute: ImageLabelAttribute?
|
||||||
|
|
||||||
|
open override var attributes: [any LabelAttributeModel]? {
|
||||||
|
guard let imageAttribute else { return nil }
|
||||||
|
return [imageAttribute]
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public var availableSizes: [ButtonSize] { [.large] }
|
public override var availableSizes: [ButtonSize] { [.large] }
|
||||||
|
|
||||||
open var text: String? { didSet { didChange() } }
|
|
||||||
|
|
||||||
open var iconPosition: TextLinkCaretPosition = .right { didSet { didChange() } }
|
open var iconPosition: TextLinkCaretPosition = .right { didSet { didChange() } }
|
||||||
|
|
||||||
private var height: CGFloat {
|
private var height: CGFloat {
|
||||||
44
|
44
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var _text: String?
|
||||||
|
|
||||||
|
open override var text: String? {
|
||||||
|
get{ _text }
|
||||||
|
set {
|
||||||
|
var updatedText = newValue ?? ""
|
||||||
|
updatedText = iconPosition == .right ? "\(updatedText) " : " \(updatedText)"
|
||||||
|
_text = updatedText
|
||||||
|
didChange()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open override var textColor: UIColor {
|
||||||
|
textColorConfiguration.getColor(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var textColorConfiguration = HighlightDisabledSurfaceColorConfiguration().with {
|
||||||
|
$0.disabled.lightColor = VDSColor.elementsSecondaryOnlight
|
||||||
|
$0.disabled.darkColor = VDSColor.elementsSecondaryOndark
|
||||||
|
$0.enabled.lightColor = VDSColor.elementsPrimaryOnlight
|
||||||
|
$0.enabled.darkColor = VDSColor.elementsPrimaryOndark
|
||||||
|
|
||||||
|
$0.highlighted.lightColor = VDSColor.interactiveActiveOnlight
|
||||||
|
$0.highlighted.darkColor = VDSColor.interactiveActiveOndark
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializers
|
// MARK: - Initializers
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
required public init() {
|
required public init() {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override init(frame: CGRect) {
|
public override init(frame: CGRect) {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public required init?(coder: NSCoder) {
|
public required init?(coder: NSCoder) {
|
||||||
super.init(coder: coder)
|
super.init(coder: coder)
|
||||||
initialSetup()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Public Functions
|
// MARK: - Public Functions
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
open override func initialSetup() {
|
|
||||||
super.initialSetup()
|
|
||||||
}
|
|
||||||
|
|
||||||
open override func setup() {
|
open override func setup() {
|
||||||
super.setup()
|
super.setup()
|
||||||
//add tapGesture to self
|
|
||||||
publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in
|
|
||||||
self?.sendActions(for: .touchUpInside)
|
|
||||||
}.store(in: &subscribers)
|
|
||||||
|
|
||||||
//constraints
|
//constraints
|
||||||
heightAnchor.constraint(greaterThanOrEqualToConstant: height).isActive = true
|
heightAnchor.constraint(greaterThanOrEqualToConstant: height).isActive = true
|
||||||
|
|
||||||
let size = caretView.size!.dimensions()
|
let size = caretView.size!.dimensions()
|
||||||
caretView.frame = .init(x: 0, y: 0, width: size.width, height: size.height)
|
caretView.frame = .init(x: 0, y: 0, width: size.width, height: size.height)
|
||||||
addSubview(label)
|
|
||||||
label.pinToSuperView()
|
|
||||||
|
|
||||||
label.numberOfLines = 1
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
|
||||||
// MARK: - Constraints
|
|
||||||
//--------------------------------------------------
|
|
||||||
private var caretLeadingConstraint: NSLayoutConstraint?
|
|
||||||
private var caretTrailingConstraint: NSLayoutConstraint?
|
|
||||||
private var labelConstraint: NSLayoutConstraint?
|
|
||||||
|
|
||||||
open override func reset() {
|
open override func reset() {
|
||||||
super.reset()
|
super.reset()
|
||||||
label.reset()
|
|
||||||
|
|
||||||
label.typograpicalStyle = TypographicalStyle.BoldBodyLarge
|
|
||||||
text = nil
|
|
||||||
iconPosition = .right
|
iconPosition = .right
|
||||||
|
|
||||||
accessibilityCustomActions = []
|
|
||||||
accessibilityTraits = .staticText
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
override open var intrinsicContentSize: CGSize {
|
override open var intrinsicContentSize: CGSize {
|
||||||
var itemWidth = label.intrinsicContentSize.width
|
var itemWidth = super.intrinsicContentSize.width
|
||||||
if let caretWidth = caretView.size?.dimensions().width {
|
if let caretWidth = caretView.size?.dimensions().width {
|
||||||
itemWidth += caretWidth
|
itemWidth += caretWidth
|
||||||
}
|
}
|
||||||
@ -121,22 +121,19 @@ open class TextLinkCaret: Control, Buttonable {
|
|||||||
|
|
||||||
open override func updateView() {
|
open override func updateView() {
|
||||||
|
|
||||||
let updatedText = text ?? ""
|
var updatedText = text ?? ""
|
||||||
|
|
||||||
caretView.surface = surface
|
caretView.surface = surface
|
||||||
caretView.disabled = disabled
|
caretView.disabled = disabled
|
||||||
caretView.direction = iconPosition == .right ? CaretView.Direction.right : CaretView.Direction.left
|
caretView.direction = iconPosition == .right ? CaretView.Direction.right : CaretView.Direction.left
|
||||||
|
|
||||||
let image = caretView.getImage()
|
let image = caretView.getImage()
|
||||||
let location = iconPosition == .right ? updatedText.count + 1 : 0
|
let location = iconPosition == .right ? updatedText.count : 0
|
||||||
let textColor = label.textColorConfiguration.getColor(self)
|
|
||||||
let imageAttribute = ImageLabelAttribute(location: location,
|
imageAttribute = ImageLabelAttribute(location: location,
|
||||||
image: image,
|
image: image,
|
||||||
tintColor: textColor)
|
tintColor: textColor)
|
||||||
label.surface = surface
|
|
||||||
label.disabled = disabled
|
super.updateView()
|
||||||
label.text = iconPosition == .right ? "\(updatedText) " : " \(updatedText)"
|
|
||||||
label.attributes = [imageAttribute]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user