# Conflicts: # VDS.xcodeproj/project.pbxproj # VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift # VDS/Components/Notification/Notification.swift # VDS/Components/TextFields/EntryFieldBase.swift # VDS/Components/TileContainer/TileContainer.swift # VDS/Extensions/VDSLayout.swift Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
99 lines
3.8 KiB
Swift
99 lines
3.8 KiB
Swift
//
|
|
// TooltipLabelAttribute.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 4/27/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import Combine
|
|
import VDSTokens
|
|
|
|
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
|
|
/// 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) {
|
|
guard isValidRange(on: attributedString) else { return }
|
|
|
|
//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.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
|
|
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
|
|
}
|
|
}
|
|
|