// // 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: Control, Buttonable { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private var heightConstraint: NSLayoutConstraint? private var label = Label() //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- open var text: String? { didSet { didChange() } } open var size: ButtonSize = .large { didSet { didChange() }} public var availableSizes: [ButtonSize] { [.large, .small] } private var height: CGFloat { switch size { case .large: return 44 case .small: return 32 } } //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- required public init() { super.init(frame: .zero) initialSetup() } public override init(frame: CGRect) { super.init(frame: .zero) initialSetup() } public required init?(coder: NSCoder) { super.init(coder: coder) initialSetup() } //-------------------------------------------------- // MARK: - Public Functions //-------------------------------------------------- open override func initialSetup() { super.initialSetup() } open override func setup() { super.setup() addSubview(label) //add tapGesture to self publisher(for: UITapGestureRecognizer()).sink { [weak self] _ in self?.sendActions(for: .touchUpInside) }.store(in: &subscribers) //pin stackview to edges label.pinToSuperView() label.numberOfLines = 1 label.preferredMaxLayoutWidth = 0 heightConstraint = heightAnchor.constraint(equalToConstant: height) heightConstraint?.isActive = true } open override func reset() { super.reset() label.reset() size = .large text = nil accessibilityCustomActions = [] accessibilityTraits = .staticText } //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- override open var intrinsicContentSize: CGSize { return CGSize(width: label.intrinsicContentSize.width, height: height) } open override func updateView() { label.surface = surface label.disabled = disabled label.typograpicalStyle = size == .large ? TypographicalStyle.BodyLarge : TypographicalStyle.BodySmall label.text = text ?? "" label.attributes = [UnderlineLabelAttribute(location: 0, length: label.text!.count)] heightConstraint?.constant = height } }