// // TrailingTooltipLabel.swift // VDS // // Created by Matt Bruce on 4/14/23. // import Foundation import UIKit import Combine @objc(VDSTrailingTooltipLabel) open class TrailingTooltipLabel: View, TooltipLaunchable { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- private let tooltipAction = PassthroughSubject() //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- open var label = Label() open var labelText: String? { didSet { didChange() }} open var labelAttributes: [any LabelAttributeModel]? { didSet { didChange() } } open var labelTextStyle: TextStyle = .defaultStyle { didSet { didChange() } } open var labelTextPosition: TextPosition = .left { didSet { didChange() } } public lazy var textColorConfiguration: AnyColorable = { label.textColorConfiguration }() { didSet { didChange() }} open var tooltipCloseButtonText: String = "Close" { didSet { didChange() } } open var tooltipSize: Tooltip.Size = .medium { didSet { didChange() } } open var tooltipTitle: String = "" { didSet { didChange() } } open var tooltipContent: String = "" { didSet { didChange() } } //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- open override func setup() { super.setup() addSubview(label) label.pinToSuperView() //create the tooltip click event tooltipAction.sink { [weak self] in guard let self else { return } self.presentTooltip(surface: self.surface, title: self.tooltipTitle, content: self.tooltipContent, closeButtonText: self.tooltipCloseButtonText) }.store(in: &subscribers) } open override func updateView() { super.updateView() var attributes: [any LabelAttributeModel] = [] if let labelAttributes { attributes.append(contentsOf: labelAttributes) } var updatedLabelText = labelText //add the tool tip if let oldText = updatedLabelText, !tooltipTitle.isEmpty, !tooltipContent.isEmpty { let tooltipUpdateText = "\(oldText) " //create a little space between the final character and tooltip image let frame = CGRect(x: 0, y: 0, width: tooltipSize.dimensions.width, height: tooltipSize.dimensions.width) let color = textColorConfiguration.getColor(self) let tooltipAttribute = ImageLabelAttribute(location: tooltipUpdateText.count - 2, imageName: "info", frame: frame, tintColor: color) let tooltipAction = ActionLabelAttribute(location: tooltipUpdateText.count - 3, length: 3, shouldUnderline: false, action: tooltipAction) updatedLabelText = tooltipUpdateText attributes.append(tooltipAttribute) attributes.append(tooltipAction) } //set the titleLabel label.text = updatedLabelText label.attributes = attributes label.textStyle = labelTextStyle label.textPosition = labelTextPosition label.surface = surface label.disabled = disabled } }