// // TextLink.swift // VDS // // Created by Matt Bruce on 11/1/22. // import Foundation import UIKit import VDSColorTokens import VDSFormControlsTokens import Combine @objc(VDSTextLink) open class TextLink: ButtonBase { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private var heightConstraint: NSLayoutConstraint? private var lineHeightConstraint: NSLayoutConstraint? //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- open var size: ButtonSize = .large { didSet { didChange() }} 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 { switch size { case .large: return 44 case .small: return 32 } } //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- required public init() { super.init(frame: .zero) } public override init(frame: CGRect) { super.init(frame: .zero) } public required init?(coder: NSCoder) { super.init(coder: coder) } private var line = UIView().with { $0.translatesAutoresizingMaskIntoConstraints = false } //-------------------------------------------------- // MARK: - Public Functions //-------------------------------------------------- open override func setup() { super.setup() if let titleLabel { addSubview(line) line.pinLeading(titleLabel.leadingAnchor) line.pinTrailing(titleLabel.trailingAnchor) line.pinTop(titleLabel.bottomAnchor, 1) lineHeightConstraint = line.heightAnchor.constraint(equalToConstant: 1.0) lineHeightConstraint?.isActive = true } heightConstraint = heightAnchor.constraint(equalToConstant: height) heightConstraint?.isActive = true } open override func reset() { super.reset() size = .large accessibilityCustomActions = [] accessibilityTraits = .staticText } //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- open override func updateView() { //need to set the properties so the super class //can render out the label correctly 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) } } }