vds_ios/VDS/Components/Label/Attributes/TooltipLabelAttribute.swift
Matt Bruce 43e9642de6 refactored to use new spacer extension
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-05-30 14:58:24 -05:00

97 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?
private var size: Tooltip.Size = .small
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 title: String
public var content: String
public func setAttribute(on attributedString: NSMutableAttributedString) {
//update the location
location = attributedString.string.count - 1
//set the default color off surface for text
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
}
var frame = CGRect.zero
if let font = attributedString.attribute(.font, at: 0, effectiveRange: &originalRange) as? UIFont {
switch font.pointSize {
case 15..<25:
size = .medium
frame = CGRect(x: 0, y: -1, width: size.value.dimensions.width * 0.80, height: size.value.dimensions.height * 0.80)
case 0..<14:
size = .small
frame = CGRect(x: 0, y: -1, width: size.value.dimensions.width * 0.80 , height: size.value.dimensions.height * 0.80)
default:
size = .medium
frame = CGRect(x: 0, y: -1, width: size.value.dimensions.width, height: size.value.dimensions.height)
}
}
//create the image icon and match the color of the text
let tooltipAttribute = ImageLabelAttribute(location: location,
imageName: "info",
frame: frame,
tintColor: imageTintColor)
let spacer = NSAttributedString.spacer(for: VDSLayout.Spacing.space1X.value)
guard let tooltip = try? tooltipAttribute.getAttachment() else { return }
attributedString.append(spacer)
attributedString.append(NSAttributedString(attachment: tooltip))
addHandler(on: attributedString)
}
public init(id: UUID = UUID(), action: PassthroughSubject<Void, Never> = PassthroughSubject<Void, Never>(), subscriber: AnyCancellable? = nil, surface: Surface, accessibleText: String? = nil, closeButtonText: String = "Close", title: String, content: String) {
self.id = id
self.action = action
self.subscriber = subscriber
self.surface = surface
self.accessibleText = accessibleText
self.closeButtonText = closeButtonText
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
}
}