WIP on textLinkCaret-images: 9676e3b Merge branch 'develop' into refactor/textLinkCaret-images Refactored to fix the issue with the contentSize of the Label

This commit is contained in:
Matt Bruce 2024-03-19 15:40:06 -05:00
commit b0a6625c49

View File

@ -121,39 +121,55 @@ open class TextLinkCaret: ButtonBase {
let space: CGFloat = VDSLayout.Spacing.space1X.value // Space between the icon and the text
var titleLabelX: CGFloat = bounds.minX + contentEdgeInsets.left
// 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
let imageSpaceWidth = imageSize.width + space
let titleWidth = titleLabel.frame.width
switch contentHorizontalAlignment {
case .center:
titleLabelX = (frame.width - titleWidth) / 2
case .right, .trailing:
titleLabelX = frame.width - titleWidth
default:
titleLabelX = imageSpaceWidth
}
titleLabel.frame.origin.x = titleLabelX
imageView.frame.origin.x = titleLabelX - imageSpaceWidth
imageView.frame.origin.y = titleLabel.frame.minY + 10 / 2.0
case .right:
guard let attribtedText = titleLabel.attributedText else { return }
titleLabel.frame.origin.x = titleLabelX
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
}
imageView.contentMode = .scaleAspectFit
}
}