Merge branch 'bugfix/label' into 'develop'

refactored action

See merge request BPHV_MIPS/vds_ios!64
This commit is contained in:
Bruce, Matt R 2023-05-08 16:07:21 +00:00
commit a0c62dcc83
5 changed files with 49 additions and 48 deletions

View File

@ -14,6 +14,12 @@ public protocol ActionLabelAttributeModel: LabelAttributeModel {
var action: PassthroughSubject<Void, Never> { get set } var action: PassthroughSubject<Void, Never> { get set }
} }
extension ActionLabelAttributeModel {
public func addHandler(on attributedString: NSMutableAttributedString){
attributedString.addAttribute(NSAttributedString.Key.action, value: "handler", range: range)
}
}
public struct ActionLabelAttribute: ActionLabelAttributeModel { public struct ActionLabelAttribute: ActionLabelAttributeModel {
public static func == (lhs: ActionLabelAttribute, rhs: ActionLabelAttribute) -> Bool { public static func == (lhs: ActionLabelAttribute, rhs: ActionLabelAttribute) -> Bool {
@ -54,7 +60,7 @@ public struct ActionLabelAttribute: ActionLabelAttributeModel {
if(shouldUnderline){ if(shouldUnderline){
UnderlineLabelAttribute(location: location, length: length).setAttribute(on: attributedString) UnderlineLabelAttribute(location: location, length: length).setAttribute(on: attributedString)
} }
attributedString.addAttribute(NSAttributedString.Key.action, value: "handler", range: range) addHandler(on: attributedString)
} }
} }

View File

@ -14,19 +14,20 @@ public class TooltipLabelAttribute: ActionLabelAttributeModel, TooltipLaunchable
public var id = UUID() public var id = UUID()
public var action = PassthroughSubject<Void, Never>() public var action = PassthroughSubject<Void, Never>()
private var subscriber: AnyCancellable? private var subscriber: AnyCancellable?
private var size: Tooltip.Size = .small
public var location: Int = 0 public var location: Int = 0
public var length: Int = 3 public var length: Int = 3
public var surface: Surface = .light public var surface: Surface = .light
public var accessibleText: String? = "Tool Tip" public var accessibleText: String? = "Tool Tip"
public var closeButtonText: String = "Close" public var closeButtonText: String = "Close"
public var size: Tooltip.Size = .medium
public var title: String public var title: String
public var content: String public var content: String
public func setAttribute(on attributedString: NSMutableAttributedString) { public func setAttribute(on attributedString: NSMutableAttributedString) {
//update the location //update the location
location = attributedString.string.count location = attributedString.string.count - 1
//set the default color off surface for text
var imageTintColor: UIColor = surface == .light ? VDSColor.elementsPrimaryOnlight : VDSColor.elementsPrimaryOndark var imageTintColor: UIColor = surface == .light ? VDSColor.elementsPrimaryOnlight : VDSColor.elementsPrimaryOndark
//see if you can get the current textColor //see if you can get the current textColor
@ -35,44 +36,49 @@ public class TooltipLabelAttribute: ActionLabelAttributeModel, TooltipLaunchable
imageTintColor = textColor imageTintColor = textColor
} }
//create the space in the attirbuted String for the tooltip image and click action var frame = CGRect.zero
let spaceForTooltip = String(repeating: " ", count: length) if let font = attributedString.attribute(.font, at: 0, effectiveRange: &originalRange) as? UIFont {
switch font.pointSize {
//find the middle of the space case 15..<25:
let middle = (length/2) size = .medium
frame = CGRect(x: 0, y: -1, width: size.value.dimensions.width * 0.80, height: size.value.dimensions.height * 0.80)
//add the space to the attributed string case 0..<14:
attributedString.insert(NSAttributedString(string: spaceForTooltip), at: location) 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 frame in which to hold the icon //create the frame in which to hold the icon
let frame = CGRect(x: 0, y: 0, width: size.value.dimensions.width, height: size.value.dimensions.width) let spacerframe = CGRect(x: 0, y: 0, width: VDSLayout.Spacing.space1X.value, height: size.value.dimensions.height)
//create the image icon and match the color of the text //create the image icon and match the color of the text
let tooltipAttribute = ImageLabelAttribute(location: location + middle, let tooltipAttribute = ImageLabelAttribute(location: location,
length: 1,
imageName: "info", imageName: "info",
frame: frame, frame: frame,
tintColor: imageTintColor) tintColor: imageTintColor)
//create the action for the tooltip click let spacerAttribute = ImageLabelAttribute(location: location,
let tooltipAction = ActionLabelAttribute(location: location - middle, imageName: "info",
length: length + middle, frame: spacerframe,
shouldUnderline: false, tintColor: .clear)
action: action)
//apply the attribtes to the current attributedString guard let tooltip = try? tooltipAttribute.getAttachment(),
tooltipAttribute.setAttribute(on: attributedString) let spacer = try? spacerAttribute.getAttachment() else { return }
tooltipAction.setAttribute(on: attributedString) attributedString.append(NSAttributedString(attachment: 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, size: Tooltip.Size, title: String, content: String) { public init(id: UUID = UUID(), action: PassthroughSubject<Void, Never> = PassthroughSubject<Void, Never>(), subscriber: AnyCancellable? = nil, surface: Surface, accessibleText: String? = nil, closeButtonText: String, title: String, content: String) {
self.id = id self.id = id
self.action = action self.action = action
self.subscriber = subscriber self.subscriber = subscriber
self.surface = surface self.surface = surface
self.accessibleText = accessibleText self.accessibleText = accessibleText
self.closeButtonText = closeButtonText self.closeButtonText = closeButtonText
self.size = size
self.title = title self.title = title
self.content = content self.content = content

View File

@ -52,6 +52,7 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
override open var text: String? { override open var text: String? {
didSet { didSet {
attributes = nil
didChange() didChange()
} }
} }

View File

@ -110,7 +110,6 @@ open class EntryField: Control, Changeable {
$0.setContentCompressionResistancePriority(.required, for: .vertical) $0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.labelTextPosition = .left $0.labelTextPosition = .left
$0.labelTextStyle = .bodySmall $0.labelTextStyle = .bodySmall
$0.tooltipSize = .small
$0.tooltipYOffset = -2 $0.tooltipYOffset = -2
} }
@ -286,7 +285,7 @@ open class EntryField: Control, Changeable {
if let oldText = updatedLabelText, !required, !oldText.hasSuffix("Optional") { if let oldText = updatedLabelText, !required, !oldText.hasSuffix("Optional") {
let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2, let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2,
length: 8, length: 8,
color: secondaryColorConfig.getColor(self)) color: .red)
updatedLabelText = "\(oldText) Optional" updatedLabelText = "\(oldText) Optional"
attributes.append(optionColorAttr) attributes.append(optionColorAttr)

View File

@ -36,8 +36,6 @@ open class TrailingTooltipLabel: View, TooltipLaunchable {
open var tooltipCloseButtonText: String = "Close" { didSet { didChange() } } open var tooltipCloseButtonText: String = "Close" { didSet { didChange() } }
open var tooltipSize: Tooltip.Size = .medium { didSet { didChange() } }
open var tooltipTitle: String = "" { didSet { didChange() } } open var tooltipTitle: String = "" { didSet { didChange() } }
open var tooltipContent: String = "" { didSet { didChange() } } open var tooltipContent: String = "" { didSet { didChange() } }
@ -68,36 +66,28 @@ open class TrailingTooltipLabel: View, TooltipLaunchable {
label.text = labelText label.text = labelText
label.textStyle = labelTextStyle label.textStyle = labelTextStyle
label.textPosition = labelTextPosition label.textPosition = labelTextPosition
label.attributes = labelAttributes
label.surface = surface label.surface = surface
label.disabled = disabled label.disabled = disabled
//add tooltip //add tooltip
if let labelText, !labelText.isEmpty, !tooltipTitle.isEmpty, !tooltipContent.isEmpty { if let labelText, !labelText.isEmpty, !tooltipTitle.isEmpty, !tooltipContent.isEmpty {
//create the model label.addTooltip(model: .init(surface: surface, closeButtonText: tooltipCloseButtonText, title: tooltipTitle, content: tooltipContent))
let model = Label.TooltipModel(surface: surface, }
closeButtonText: tooltipCloseButtonText, }
size: tooltipSize, }
title: tooltipTitle,
content: tooltipContent)
//add the model
label.addTooltip(model: model)
}
}
}
extension Label { extension Label {
public struct TooltipModel { public struct TooltipModel {
public var surface: Surface public var surface: Surface
public var closeButtonText: String public var closeButtonText: String
public var size: Tooltip.Size
public var title: String public var title: String
public var content: String public var content: String
public init(surface: Surface = .light, closeButtonText: String = "Close", size: Tooltip.Size = .medium, title: String, content: String) { public init(surface: Surface = .light, closeButtonText: String = "Close", title: String, content: String) {
self.surface = surface self.surface = surface
self.closeButtonText = closeButtonText self.closeButtonText = closeButtonText
self.size = size
self.title = title self.title = title
self.content = content self.content = content
} }
@ -117,7 +107,6 @@ extension Label {
if let text = text, !text.isEmpty { if let text = text, !text.isEmpty {
let tooltip = TooltipLabelAttribute(surface: surface, let tooltip = TooltipLabelAttribute(surface: surface,
closeButtonText: model.closeButtonText, closeButtonText: model.closeButtonText,
size: model.size,
title: model.title, title: model.title,
content: model.content) content: model.content)
newAttributes.append(tooltip) newAttributes.append(tooltip)