diff --git a/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift b/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift index fd990720..b304a99a 100644 --- a/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift +++ b/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift @@ -80,11 +80,20 @@ open class TextLinkCaret: ButtonBase { open override func setup() { super.setup() accessibilityTraits = .link + titleLabel?.numberOfLines = 0 + if let caret = BundleManager.shared.image(for: Icon.Name.leftCaretBold.rawValue) { + setImage(caret, for: .normal) + } } /// Used to make changes to the View based off a change events or from local properties. open override func updateView() { - imageAttribute = CaretLabelAttribute(tintColor: textColor, position: iconPosition) + if iconPosition == .right { + imageAttribute = CaretLabelAttribute(tintColor: textColor, position: iconPosition) + } else { + imageAttribute = nil + } + imageView?.isHidden = iconPosition == .right super.updateView() } @@ -98,8 +107,74 @@ open class TextLinkCaret: ButtonBase { /// The natural size for the receiving view, considering only properties of the view itself. override open var intrinsicContentSize: CGSize { //get the labels size, if not the button - return titleLabel?.intrinsicContentSize ?? super.intrinsicContentSize + if iconPosition == .right { + return titleLabel?.intrinsicContentSize ?? super.intrinsicContentSize + } else { + let width = imageSize.width + 4.0 + (titleLabel?.intrinsicContentSize.width ?? super.intrinsicContentSize.width) + let height = titleLabel?.intrinsicContentSize.height ?? super.intrinsicContentSize.height + return .init(width: width, height: height) + } } + private let imageSize = Icon.Size.xsmall.dimensions + private let spacing = 4.0 +// private var activeConstraints: [NSLayoutConstraint] = [] +// +// private func setupConstraints() { +// guard let titleLabel else { return } +// titleLabel.translatesAutoresizingMaskIntoConstraints = false +// +// NSLayoutConstraint.deactivate(activeConstraints) +// activeConstraints.removeAll() +// +// if let caret = BundleManager.shared.image(for: Icon.Name.leftCaretBold.rawValue), iconPosition == .left{ +// setImage(caret, for: .normal) +// guard let imageView else { return } +// imageView.removeConstraints(imageView.constraints) +// imageView.translatesAutoresizingMaskIntoConstraints = false +// +// activeConstraints = [ +// imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor), +// imageView.topAnchor.constraint(equalTo: titleLabel.topAnchor, constant: 5), +// imageView.widthAnchor.constraint(equalToConstant: imageSize.width), +// imageView.heightAnchor.constraint(equalToConstant: imageSize.height), +// titleLabel.leadingAnchor.constraint(equalTo: imageView.trailingAnchor), +// titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor), +// titleLabel.topAnchor.constraint(equalTo: self.topAnchor), +// titleLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor) +// ] +// +// } else { +// +// setImage(nil, for: .normal) +// activeConstraints = [ +// titleLabel.topAnchor.constraint(equalTo: self.topAnchor), +// titleLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor), +// titleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor), +// titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor) +// ] +// } +// +// NSLayoutConstraint.activate(activeConstraints) +// } + + open override func layoutSubviews() { + super.layoutSubviews() + + guard let imageView, let titleLabel else { return } + + if imageView.isHidden { + titleLabel.frame.origin.x = 0 + contentEdgeInsets = .zero + } else { + imageView.frame.origin.x = 0 + titleLabel.frame.origin.x = imageSize.width + spacing + + let totalWidth = titleLabel.frame.maxX + let leftInset = (bounds.width - totalWidth) / 2 + contentEdgeInsets = .init(top: 0, left: leftInset, bottom: 0, right: leftInset) + } + } + } extension TextLinkCaret { @@ -127,9 +202,6 @@ extension TextLinkCaret { if position == .right { attributedString.append(spacer) attributedString.append(NSAttributedString(attachment: image)) - } else { - attributedString.insert(NSAttributedString(attachment: image), at: 0) - attributedString.insert(spacer, at: 1) } }