From f99a3033d67e7b7c681a0173d4c2361644c9a0b3 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 30 May 2023 10:05:23 -0500 Subject: [PATCH] refactored TextLinkCaret spacer/name label attribute Signed-off-by: Matt Bruce --- VDS.xcodeproj/project.pbxproj | 4 ++++ .../Buttons/TextLinkCaret/TextLinkCaret.swift | 18 +++++++++--------- VDS/Extensions/NSAttributedString.swift | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 VDS/Extensions/NSAttributedString.swift diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 2f938f80..bb1023cc 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -95,6 +95,7 @@ EAB5FEF5292D371F00998C17 /* ButtonBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB5FEF4292D371F00998C17 /* ButtonBase.swift */; }; EAB5FEF829393A7200998C17 /* ButtonGroupConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB5FEF729393A7200998C17 /* ButtonGroupConstants.swift */; }; EAB5FF0129424ACB00998C17 /* UIControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB5FF0029424ACB00998C17 /* UIControl.swift */; }; + EABFEB642A26473700C4C106 /* NSAttributedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = EABFEB632A26473700C4C106 /* NSAttributedString.swift */; }; EAC846F3294B95CE00F685BA /* ButtonGroupCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC846F2294B95CE00F685BA /* ButtonGroupCollectionViewCell.swift */; }; EAC9257D29119B5400091998 /* TextLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC9257C29119B5400091998 /* TextLink.swift */; }; EAC925832911B35400091998 /* TextLinkCaret.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAC925822911B35300091998 /* TextLinkCaret.swift */; }; @@ -222,6 +223,7 @@ EAB5FEF4292D371F00998C17 /* ButtonBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonBase.swift; sourceTree = ""; }; EAB5FEF729393A7200998C17 /* ButtonGroupConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonGroupConstants.swift; sourceTree = ""; }; EAB5FF0029424ACB00998C17 /* UIControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIControl.swift; sourceTree = ""; }; + EABFEB632A26473700C4C106 /* NSAttributedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSAttributedString.swift; sourceTree = ""; }; EAC846F2294B95CE00F685BA /* ButtonGroupCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonGroupCollectionViewCell.swift; sourceTree = ""; }; EAC9257C29119B5400091998 /* TextLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextLink.swift; sourceTree = ""; }; EAC925822911B35300091998 /* TextLinkCaret.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextLinkCaret.swift; sourceTree = ""; }; @@ -418,6 +420,7 @@ children = ( EAF7F0992899B17200B287F5 /* CATransaction.swift */, EA33622D2891EA3C0071C351 /* DispatchQueue+Once.swift */, + EABFEB632A26473700C4C106 /* NSAttributedString.swift */, EAB2376529E9952D00AABE9A /* UIApplication.swift */, EA3361A7288B23300071C351 /* UIColor.swift */, EA81410F2A127066004F60D2 /* UIColor+VDSColor.swift */, @@ -871,6 +874,7 @@ EAF7F0B1289B177F00B287F5 /* ColorLabelAttribute.swift in Sources */, EAC9258F2911C9DE00091998 /* EntryField.swift in Sources */, EAB1D2EA28AE84AA00DAE764 /* UIControlPublisher.swift in Sources */, + EABFEB642A26473700C4C106 /* NSAttributedString.swift in Sources */, EAF7F13328A2A16500B287F5 /* AttachmentLabelAttributeModel.swift in Sources */, EA0FC2C62914222900DF80B4 /* ButtonGroup.swift in Sources */, EA89200628B526D6006B9984 /* CheckboxGroup.swift in Sources */, diff --git a/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift b/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift index a7dd808e..ed741896 100644 --- a/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift +++ b/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift @@ -28,7 +28,7 @@ open class TextLinkCaret: ButtonBase { TextStyle.boldBodyLarge } - private var imageAttribute: ImageSpaceLabelAttribute? + private var imageAttribute: CaretLabelAttribute? open override var attributes: [any LabelAttributeModel]? { guard let imageAttribute else { return nil } @@ -100,14 +100,14 @@ open class TextLinkCaret: ButtonBase { } open override func updateView() { - imageAttribute = ImageSpaceLabelAttribute(tintColor: textColor, position: iconPosition) + imageAttribute = CaretLabelAttribute(tintColor: textColor, position: iconPosition) super.updateView() } } extension TextLinkCaret { - struct ImageSpaceLabelAttribute: LabelAttributeModel { + struct CaretLabelAttribute: LabelAttributeModel { var id: UUID = .init() var location: Int = 0 var length: Int = 1 @@ -124,22 +124,22 @@ extension TextLinkCaret { func setAttribute(on attributedString: NSMutableAttributedString) { let imageAttr = ImageLabelAttribute(location: location, imageName: "\(position.rawValue)-caret-bold", frame: .init(x: 0, y: 0, width: caretSize.width, height: caretSize.height), tintColor: tintColor) - let spaceAttr = ImageLabelAttribute(location: 0, imageName: "info", frame: .init(x: 0, y: 0, width: spacerWidth, height: 5.0), tintColor: .clear) + let spacer = NSAttributedString.spacer(for: spacerWidth) - guard let image = try? imageAttr.getAttachment(), - let spacer = try? spaceAttr.getAttachment() else { return } + guard let image = try? imageAttr.getAttachment() else { return } if position == .right { - attributedString.append(NSAttributedString(attachment: spacer)) + attributedString.append(spacer) attributedString.append(NSAttributedString(attachment: image)) } else { attributedString.insert(NSAttributedString(attachment: image), at: 0) - attributedString.insert(NSAttributedString(attachment: spacer), at: 1) + attributedString.insert(spacer, at: 1) } } - func isEqual(_ equatable: ImageSpaceLabelAttribute) -> Bool { + func isEqual(_ equatable: CaretLabelAttribute) -> Bool { return id == equatable.id && range == equatable.range } + } } diff --git a/VDS/Extensions/NSAttributedString.swift b/VDS/Extensions/NSAttributedString.swift new file mode 100644 index 00000000..d51ec05a --- /dev/null +++ b/VDS/Extensions/NSAttributedString.swift @@ -0,0 +1,19 @@ +// +// NSAttributedString.swift +// VDS +// +// Created by Matt Bruce on 5/30/23. +// + +import Foundation +import UIKit + +extension NSAttributedString { + public static func spacer(for width: CGFloat) -> NSAttributedString { + let spacerImage = UIImage() + let spacerAttachment = NSTextAttachment() + spacerAttachment.bounds = .init(x: 0, y: 0, width: width, height: 1) + spacerAttachment.image = spacerImage + return NSAttributedString(attachment: spacerAttachment) + } +}