// // TooltipLabelAttribute.swift // VDS // // Created by Matt Bruce on 4/27/23. // import Foundation import UIKit import Combine import VDSCoreTokens public class TooltipLabelAttribute: ActionLabelAttributeModel, TooltipLaunchable { public var id = UUID() public var action = PassthroughSubject() private var subscriber: AnyCancellable? private var size: Tooltip.Size = .small public var location: Int = 0 public var length: Int = 3 /// Current Surface and this is used to pass down to child objects that implement Surfacable public var surface: Surface = .light public var accessibleText: String? = "Tool Tip" public var model: Tooltip.TooltipModel public var presenter: UIView? 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 let ratio: Double = 1.0 //0.80 let yPosition: Double = -3 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: yPosition, width: size.value.dimensions.width * ratio, height: size.value.dimensions.height * ratio) case 0..<14: size = .small frame = CGRect(x: 0, y: yPosition, width: size.value.dimensions.width * ratio , height: size.value.dimensions.height * ratio) default: size = .medium frame = CGRect(x: 0, y: yPosition, 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.space1X) guard let tooltip = try? tooltipAttribute.getAttachment() else { return } attributedString.append(spacer) attributedString.append(NSAttributedString(attachment: tooltip)) addHandler(on: attributedString) } public init(id: UUID = UUID(), subscriber: AnyCancellable? = nil, accessibleText: String? = nil, surface: Surface = .light, model: Tooltip.TooltipModel, presenter: UIView? = nil) { self.id = id self.subscriber = subscriber self.surface = surface self.model = model self.accessibleText = accessibleText ?? model.accessibleText self.presenter = presenter //create the tooltip click event self.subscriber = action.sink { [weak self] in guard let self else { return } self.presentTooltip(surface: surface, tooltipModel: model, presenter: self.presenter) } } public static func == (lhs: TooltipLabelAttribute, rhs: TooltipLabelAttribute) -> Bool { lhs.isEqual(rhs) } public func isEqual(_ equatable: TooltipLabelAttribute) -> Bool { return id == equatable.id && range == equatable.range } }