From d611960ac8e36cbe4926d44d3f1a8cf615e88cdd Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 2 Nov 2022 11:04:49 -0500 Subject: [PATCH] refactored entryfield Signed-off-by: Matt Bruce --- .../TextFields/EntryField/EntryField.swift | 64 ++++++++----------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/VDS/Components/TextFields/EntryField/EntryField.swift b/VDS/Components/TextFields/EntryField/EntryField.swift index f0be2f88..8ed3782d 100644 --- a/VDS/Components/TextFields/EntryField/EntryField.swift +++ b/VDS/Components/TextFields/EntryField/EntryField.swift @@ -44,6 +44,7 @@ open class EntryField: Control, Accessable { internal var titleLabel = Label().with { $0.setContentCompressionResistancePriority(.required, for: .vertical) + $0.attributes = [] } internal var errorLabel = Label().with { @@ -273,23 +274,36 @@ open class EntryField: Control, Accessable { setAccessibilityHint() backgroundColor = surface.color - setNeedsLayout() - layoutIfNeeded() } open func updateTitleLabel() { + + //update the local vars for the label since we no + //long have a model + var attributes: [any LabelAttributeModel] = [] + var updatedLabelText = labelText - //remove all attributes - titleLabel.attributes?.removeAll() + //dealing with the "Optional" addition to the text + if let oldText = updatedLabelText, !required, !oldText.hasSuffix("Optional") { + let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2, + length: 8, + color: secondaryColorConfig.getColor(self)) + + updatedLabelText = "\(oldText) Optional" + attributes.append(optionColorAttr) + } - //update the title model if the required flag is false - titleLabel.addOptional(required: required, colorConfiguration: secondaryColorConfig) - - //tooltip action - if let view = getToolTipView() { + //add the tool tip + if let view = getToolTipView(), let oldText = updatedLabelText { tooltipView = view let toolTipAction = PassthroughSubject() - titleLabel.addToolTip(action: toolTipAction, colorConfiguration: primaryColorConfig) + let toolTipUpdateText = "\(oldText) " //create a little space between the final character and tooltip image + let toolTipAttribute = ToolTipLabelAttribute(action: toolTipAction, + location: toolTipUpdateText.count, + length: 1, + tintColor: primaryColorConfig.getColor(self)) + updatedLabelText = toolTipUpdateText + attributes.append(toolTipAttribute) toolTipAction.sink { [weak self] in self?.showToolTipView() }.store(in: &subscribers) @@ -297,9 +311,11 @@ open class EntryField: Control, Accessable { tooltipView = nil } + //set the titleLabel titleLabel.textPosition = .left titleLabel.typograpicalStyle = .BodySmall - titleLabel.text = labelText + titleLabel.text = updatedLabelText + titleLabel.attributes = attributes titleLabel.surface = surface titleLabel.disabled = disabled @@ -355,29 +371,3 @@ internal class ErrorDisabledSurfaceColorConfiguration: DisabledSurfaceColorable } } } - -extension Label { - public func addOptional(required: Bool, colorConfiguration: DisabledSurfaceColorConfiguration) { - guard let oldText = text, !required, !oldText.hasSuffix("Optional") else { return } - let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2, - length: 8, - color: colorConfiguration.getColor(self)) - - let newText = "\(oldText) Optional" - text = newText - attributes = [optionColorAttr] - } - - public func addToolTip(action: PassthroughSubject, colorConfiguration: DisabledSurfaceColorConfiguration) { - guard let oldText = text else { return} - var newAttributes = attributes ?? [] - let newText = "\(oldText) " //create a little space between the final character and tooltip image - let toolTip = ToolTipLabelAttribute(action: action, - location: newText.count, - length: 1, - tintColor: colorConfiguration.getColor(self)) - newAttributes.append(toolTip) - text = newText - attributes = newAttributes - } -}