vds_ios/VDS/Components/Tooltip/TrailingTooltipLabel.swift
Matt Bruce f6af646d09 refactored tooltip/label to use protocol
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-04-14 14:54:24 -05:00

91 lines
3.0 KiB
Swift

//
// 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<Void, Never>()
//--------------------------------------------------
// 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() }}
public lazy var textColorConfiguration: AnyColorable = {
label.textColorConfiguration
}() { didSet { didChange() }}
open var closeButtonText: 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.closeButtonText)
}.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 {
let toolTipUpdateText = "\(oldText) " //create a little space between the final character and tooltip image
let toolTipAttribute = ToolTipLabelAttribute(action: toolTipAction,
location: toolTipUpdateText.count - 1,
length: 1,
tintColor: textColorConfiguration.getColor(self))
updatedLabelText = toolTipUpdateText
attributes.append(toolTipAttribute)
}
//set the titleLabel
label.text = updatedLabelText
label.attributes = attributes
label.textStyle = labelTextStyle
label.surface = surface
label.disabled = disabled
}
}