Compare commits

...

2 Commits

Author SHA1 Message Date
Matt Bruce
9676e3b4d0 Merge branch 'develop' into refactor/textLinkCaret-images
Refactored to fix the issue with the contentSize of the Label

# Conflicts:
#	VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift


Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-03-19 09:49:42 -05:00
Matt Bruce
277a67184f added images
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-03-18 16:55:28 -05:00

View File

@ -40,30 +40,29 @@ open class TextLinkCaret: ButtonBase {
/// Enum used to describe the position of the icon in relation to the title label. /// Enum used to describe the position of the icon in relation to the title label.
public enum IconPosition: String, CaseIterable { public enum IconPosition: String, CaseIterable {
case left, right case left, right
var image: UIImage {
UIImage.image(for: self == .left ? .leftCaretBold : .rightCaretBold)!
}
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
// Property to specify the icon size
private var imageSize: CGSize = Icon.Size.xsmall.dimensions
private var textColorConfiguration = ControlColorConfiguration().with { private var textColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal) $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
$0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled) $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forState: .disabled)
$0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted) $0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted)
} }
private var imageAttribute: CaretLabelAttribute?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Properties // MARK: - Public Properties
//-------------------------------------------------- //--------------------------------------------------
/// Determines icon position of Caret. /// Determines icon position of Caret.
open var iconPosition: IconPosition = .right { didSet { setNeedsUpdate() } } open var iconPosition: IconPosition = .right { didSet { setNeedsUpdate() } }
open override var textAttributes: [any LabelAttributeModel]? {
guard let imageAttribute else { return nil }
return [imageAttribute]
}
/// UIColor used on the titleLabel text. /// UIColor used on the titleLabel text.
open override var textColor: UIColor { open override var textColor: UIColor {
textColorConfiguration.getColor(self) textColorConfiguration.getColor(self)
@ -72,7 +71,7 @@ open class TextLinkCaret: ButtonBase {
open override var textStyle: TextStyle { open override var textStyle: TextStyle {
TextStyle.boldBodyLarge TextStyle.boldBodyLarge
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Overrides // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
@ -83,7 +82,7 @@ open class TextLinkCaret: ButtonBase {
//left align titleLabel in case this is pinned leading/trailing //left align titleLabel in case this is pinned leading/trailing
//default is always set to center //default is always set to center
contentHorizontalAlignment = .left contentHorizontalAlignment = .left
accessibilityTraits = .link accessibilityTraits = .link
titleLabel?.numberOfLines = 0 titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = .byWordWrapping titleLabel?.lineBreakMode = .byWordWrapping
@ -91,7 +90,7 @@ open class TextLinkCaret: ButtonBase {
/// Used to make changes to the View based off a change events or from local properties. /// Used to make changes to the View based off a change events or from local properties.
open override func updateView() { open override func updateView() {
imageAttribute = CaretLabelAttribute(tintColor: textColor, position: iconPosition) setImage(iconPosition.image.withTintColor(textColor), for: .normal)
super.updateView() super.updateView()
} }
@ -101,61 +100,60 @@ open class TextLinkCaret: ButtonBase {
iconPosition = .right iconPosition = .right
text = nil text = nil
} }
/// The natural size for the receiving view, considering only properties of the view itself. /// Retuns the correct CGSize since this could contain a Multiline Label.
open override var intrinsicContentSize: CGSize { open override var intrinsicContentSize: CGSize {
guard let titleLabel else { return super.intrinsicContentSize } let labelSize = titleLabel?.sizeThatFits(CGSize(width: self.frame.width - (contentEdgeInsets.left + contentEdgeInsets.right + imageSize.width), height: CGFloat.greatestFiniteMagnitude)) ?? .zero
// Calculate the titleLabel's intrinsic content size let contentWidth = labelSize.width + contentEdgeInsets.left + contentEdgeInsets.right + imageSize.width
let labelSize = titleLabel.sizeThatFits(CGSize(width: self.frame.width - (contentEdgeInsets.left + contentEdgeInsets.right), height: CGFloat.greatestFiniteMagnitude)) let contentHeight = max(labelSize.height, imageSize.height) + contentEdgeInsets.top + contentEdgeInsets.bottom
// Adjust the size if needed (add any additional padding if your design requires)
let adjustedSize = CGSize(width: labelSize.width + contentEdgeInsets.left + contentEdgeInsets.right, return CGSize(width: contentWidth, height: contentHeight)
height: labelSize.height + contentEdgeInsets.top + contentEdgeInsets.bottom)
return adjustedSize
} }
/// Layouts the correct positioning of the Caret Image within ``TextLinkCaret`` based of the `iconPosition`.
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
// This ensures the titleLabel is correctly positioned within the button guard let titleLabel = titleLabel, let imageView = imageView else { return }
titleLabel?.preferredMaxLayoutWidth = self.frame.width - (contentEdgeInsets.left + contentEdgeInsets.right)
super.layoutSubviews() // Calling super again to ensure layout is updated with preferredMaxLayoutWidth // Adjust imageView size based on the imageSize property
} imageView.frame.size = imageSize
titleLabel.preferredMaxLayoutWidth = self.frame.width - (contentEdgeInsets.left + contentEdgeInsets.right + imageSize.width)
} let space: CGFloat = VDSLayout.Spacing.space1X.value // Space between the icon and the text
// Adjust icon and text positions based on the iconPosition
switch iconPosition {
case .left:
imageView.frame.origin.x = bounds.minX + contentEdgeInsets.left
imageView.frame.origin.y = titleLabel.frame.minY + (textStyle.lineHeight - imageSize.height) / 2.0
titleLabel.frame.origin.x = imageView.frame.maxX + space
case .right:
guard let attribtedText = titleLabel.attributedText else { return }
let textContainer = NSTextContainer(size: CGSize(width: titleLabel.bounds.width, height: CGFloat.greatestFiniteMagnitude))
textContainer.lineFragmentPadding = 0
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
let textStorage = NSTextStorage(attributedString: attribtedText)
textStorage.addLayoutManager(layoutManager)
let lastGlyphIndex = layoutManager.glyphIndexForCharacter(at: attribtedText.string.utf16.count - 1)
var lastGlyphRect = layoutManager.boundingRect(forGlyphRange: NSRange(location: lastGlyphIndex, length: 1), in: textContainer)
lastGlyphRect.origin.x += titleLabel.frame.origin.x
lastGlyphRect.origin.y += titleLabel.frame.origin.y
imageView.frame.origin.x = lastGlyphRect.maxX + space
imageView.frame.origin.y = lastGlyphRect.midY - imageSize.height / 2
extension TextLinkCaret {
struct CaretLabelAttribute: LabelAttributeModel {
var id: UUID = .init()
var location: Int = 0
var length: Int = 1
var tintColor: UIColor
var position: IconPosition
var spacerWidth: CGFloat = 4.0
var width: CGFloat { caretSize.width + spacerWidth }
var caretSize: CGSize { Icon.Size.xsmall.dimensions }
init(tintColor: UIColor, position: IconPosition) {
self.tintColor = tintColor
self.position = position
} }
func setAttribute(on attributedString: NSMutableAttributedString) { imageView.contentMode = .scaleAspectFit
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 spacer = NSAttributedString.spacer(for: spacerWidth)
guard let image = try? imageAttr.getAttachment() else { return }
if position == .right {
attributedString.append(spacer)
attributedString.append(NSAttributedString(attachment: image))
} else {
attributedString.insert(NSAttributedString(attachment: image), at: 0)
attributedString.insert(spacer, at: 1)
}
}
func isEqual(_ equatable: CaretLabelAttribute) -> Bool {
return id == equatable.id && range == equatable.range
}
} }
} }