// // TrailingTooltipLabel.swift // VDS // // Created by Matt Bruce on 4/14/23. // import Foundation import UIKit import Combine /// A trailing tooltip is view that contains a label that has a tooltip overlay /// applied at the last character of the text. @objc(VDSTrailingTooltipLabel) open class TrailingTooltipLabel: View, TooltipLaunchable { //-------------------------------------------------- // 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) } //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- /// Label used to render the label text. open var label = Label() /// Text used to render the label. open var labelText: String? { didSet { setNeedsUpdate() } } /// Attributes used to render the label. open var labelAttributes: [any LabelAttributeModel]? { didSet { setNeedsUpdate() } } /// Text style used to render the label. open var labelTextStyle: TextStyle = .defaultStyle { didSet { setNeedsUpdate() } } /// Text position used to render the label. open var labelTextAlignment: TextAlignment = .left { didSet { setNeedsUpdate() } } /// Color configuration set for the label. public lazy var textColorConfiguration: AnyColorable = { label.textColorConfiguration }() { didSet { setNeedsUpdate() } } /// Will render the text for Close button for tooltip dialog when on mobile devices open var tooltipModel: Tooltip.TooltipModel? { didSet { setNeedsUpdate() } } //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. open override func setup() { super.setup() addSubview(label) label.pinToSuperView() } /// Used to make changes to the View based off a change events or from local properties. open override func updateView() { super.updateView() label.text = labelText label.textStyle = labelTextStyle label.textAlignment = labelTextAlignment.value label.attributes = labelAttributes label.surface = surface label.isEnabled = isEnabled //add tooltip if let labelText, let tooltipModel, !labelText.isEmpty { label.addTooltip(model: tooltipModel) } } /// Resets to default settings. open override func reset() { super.reset() shouldUpdateView = false labelText = nil labelAttributes = nil labelTextStyle = .defaultStyle labelTextAlignment = .left tooltipModel = nil shouldUpdateView = true setNeedsUpdate() } } extension Label { /// Helper to add a tool tip attribute to an existing label. public func addTooltip(model: Tooltip.TooltipModel) { var newAttributes: [any LabelAttributeModel] = [] if let attributes { attributes.forEach { attribute in if type(of: attribute) != TooltipLabelAttribute.self { newAttributes.append(attribute) } } } if let text = text, !text.isEmpty { let tooltip = TooltipLabelAttribute(surface: surface, model: model, presenter: self) newAttributes.append(tooltip) } if !newAttributes.isEmpty { attributes = newAttributes } } }