vds_ios/VDS/Components/Label/Attributes/TooltipLabelAttribute.swift
Matt Bruce 90974afb8d added tooltiplabelattribute
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-04-27 14:46:04 -05:00

98 lines
3.9 KiB
Swift

//
// TooltipLabelAttribute.swift
// VDS
//
// Created by Matt Bruce on 4/27/23.
//
import Foundation
import UIKit
import Combine
import VDSColorTokens
public class TooltipLabelAttribute: ActionLabelAttributeModel, TooltipLaunchable {
public var id = UUID()
public var action = PassthroughSubject<Void, Never>()
private var subscriber: AnyCancellable?
public var location: Int = 0
public var length: Int = 3
public var surface: Surface = .light
public var accessibleText: String? = "Tool Tip"
public var closeButtonText: String = "Close"
public var size: Tooltip.Size = .medium
public var title: String
public var content: String
public func setAttribute(on attributedString: NSMutableAttributedString) {
//update the location
location = attributedString.string.count
var imageTintColor: UIColor = surface == .light ? VDSColor.elementsPrimaryOnlight : VDSColor.elementsPrimaryOndark
//see if you can get the current textColor
var originalRange = NSMakeRange(0, attributedString.length)
if let textColor = attributedString.attribute(.foregroundColor, at: 0, effectiveRange: &originalRange) as? UIColor {
imageTintColor = textColor
}
//create the space in the attirbuted String for the tooltip image and click action
let spaceForTooltip = String(repeating: " ", count: length)
//find the middle of the space
let middle = (length/2)
//add the space to the attributed string
attributedString.insert(NSAttributedString(string: spaceForTooltip), at: location)
//create the frame in which to hold the icon
let frame = CGRect(x: 0, y: 0, width: size.dimensions.width, height: size.dimensions.width)
//create the image icon and match the color of the text
let tooltipAttribute = ImageLabelAttribute(location: location + middle,
length: 1,
imageName: "info",
frame: frame,
tintColor: imageTintColor)
//create the action for the tooltip click
let tooltipAction = ActionLabelAttribute(location: location - middle,
length: length + middle,
shouldUnderline: false,
action: action)
//apply the attribtes to the current attributedString
tooltipAttribute.setAttribute(on: attributedString)
tooltipAction.setAttribute(on: attributedString)
}
public init(id: UUID = UUID(), action: PassthroughSubject<Void, Never> = PassthroughSubject<Void, Never>(), subscriber: AnyCancellable? = nil, surface: Surface, accessibleText: String? = nil, closeButtonText: String, size: Tooltip.Size, title: String, content: String) {
self.id = id
self.action = action
self.subscriber = subscriber
self.surface = surface
self.accessibleText = accessibleText
self.closeButtonText = closeButtonText
self.size = size
self.title = title
self.content = content
//create the tooltip click event
self.subscriber = action.sink { [weak self] in
guard let self else { return }
self.presentTooltip(surface: self.surface,
title: self.title,
content: self.content,
closeButtonText: self.closeButtonText)
}
}
public static func == (lhs: TooltipLabelAttribute, rhs: TooltipLabelAttribute) -> Bool {
lhs.isEqual(rhs)
}
public func isEqual(_ equatable: TooltipLabelAttribute) -> Bool {
return id == equatable.id && range == equatable.range
}
}