// // 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() 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 var contentView: UIView? 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 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 = PassthroughSubject(), subscriber: AnyCancellable? = nil, surface: Surface, accessibleText: String? = nil, closeButtonText: String = "Close", title: String? = nil, content: String? = nil, contentView: UIView? = nil, presenter: UIView? = nil) { self.id = id self.action = action self.subscriber = subscriber self.surface = surface self.accessibleText = accessibleText self.closeButtonText = closeButtonText self.title = title self.content = content self.contentView = contentView self.presenter = presenter //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, contentView: contentView, closeButtonText: self.closeButtonText, 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 } }