refactored TextLinkCaret spacer/name label attribute

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-05-30 10:05:23 -05:00
parent 9a8f45a002
commit f99a3033d6
3 changed files with 32 additions and 9 deletions

View File

@ -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 = "<group>"; };
EAB5FEF729393A7200998C17 /* ButtonGroupConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonGroupConstants.swift; sourceTree = "<group>"; };
EAB5FF0029424ACB00998C17 /* UIControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIControl.swift; sourceTree = "<group>"; };
EABFEB632A26473700C4C106 /* NSAttributedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSAttributedString.swift; sourceTree = "<group>"; };
EAC846F2294B95CE00F685BA /* ButtonGroupCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonGroupCollectionViewCell.swift; sourceTree = "<group>"; };
EAC9257C29119B5400091998 /* TextLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextLink.swift; sourceTree = "<group>"; };
EAC925822911B35300091998 /* TextLinkCaret.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextLinkCaret.swift; sourceTree = "<group>"; };
@ -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 */,

View File

@ -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
}
}
}

View File

@ -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)
}
}