Merge branch 'bugfix/11_4_0' into 'release/11_4_0'

update for TitleLockup for label compression/hugging

See merge request BPHV_MIPS/vds_ios!172
This commit is contained in:
Bruce, Matt R 2024-03-11 18:58:13 +00:00
commit 65da3ee8cd
3 changed files with 25 additions and 13 deletions

View File

@ -34,10 +34,17 @@ public final class SelfSizingCollectionView: UICollectionView {
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var contentSizeObservation: NSKeyValueObservation?
private var collectionViewHeight: NSLayoutConstraint?
private var anyCancellable: AnyCancellable?
private var contentSizeSubject = CurrentValueSubject<CGSize, Never>(.zero)
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
public var contentSizePublisher: AnyPublisher<CGSize, Never> {
contentSizeSubject.eraseToAnyPublisher()
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
@ -45,7 +52,6 @@ public final class SelfSizingCollectionView: UICollectionView {
/// The natural size for the receiving view, considering only properties of the view itself.
public override var intrinsicContentSize: CGSize {
let contentSize = self.contentSize
//print(#function, contentSize)
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
@ -68,17 +74,16 @@ public final class SelfSizingCollectionView: UICollectionView {
setContentHuggingPriority(.required, for: .vertical)
setContentCompressionResistancePriority(.required, for: .vertical)
collectionViewHeight = heightAnchor.constraint(equalToConstant: 0).activate()
// Observing the value of contentSize seems to be the only reliable way to get the contentSize after the collection view lays out its subviews.
self.contentSizeObservation = self.observe(\.contentSize, options: [.old, .new]) { [weak self] _, change in
// If we don't specify `options: [.old, .new]`, the change.oldValue and .newValue will always be `nil`.
if change.newValue != change.oldValue {
self?.invalidateIntrinsicContentSize()
if let height = change.newValue?.height {
self?.collectionViewHeight?.constant = height
anyCancellable = self.publisher(for: \.contentSize, options: [.new])
.sink { [weak self] compare in
guard let self else { return }
if compare.height != self.collectionViewHeight?.constant {
self.invalidateIntrinsicContentSize()
self.collectionViewHeight?.constant = compare.height
self.contentSizeSubject.send(compare)
}
}
}
}
}

View File

@ -9,6 +9,7 @@ import Foundation
import UIKit
import VDSColorTokens
import VDSFormControlsTokens
import Combine
/// A button group contains combinations of related CTAs including ``Button``, ``TextLink``, and ``TextLinkCaret``. This group component controls a combination's orientation, spacing, size and allowable size pairings.
@objc(VDSButtonGroup)
@ -98,6 +99,8 @@ open class ButtonGroup: View {
buttons.forEach { $0.surface = surface }
}
}
open var contentSizePublisher: AnyPublisher<CGSize, Never> { collectionView.contentSizePublisher }
//--------------------------------------------------
// MARK: - Private Properties
@ -108,6 +111,7 @@ open class ButtonGroup: View {
$0.delegate = self
}
/// CollectionView that renders the array of buttonBase obects.
fileprivate lazy var collectionView: SelfSizingCollectionView = {
return SelfSizingCollectionView(frame: .zero, collectionViewLayout: positionLayout).with {

View File

@ -68,6 +68,7 @@ open class TitleLockup: View {
/// Label used to render the eyebrow model.
open var eyebrowLabel = Label().with {
$0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.setContentHuggingPriority(.required, for: .vertical)
}
/// Model used in rendering the eyebrow label.
@ -77,6 +78,7 @@ open class TitleLockup: View {
/// Label used to render the title model.
open var titleLabel = Label().with {
$0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.setContentHuggingPriority(.required, for: .vertical)
$0.accessibilityTraits.insert([.header])
}
@ -87,6 +89,7 @@ open class TitleLockup: View {
/// Label used to render the subtitle model.
open var subTitleLabel = Label().with {
$0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.setContentHuggingPriority(.required, for: .vertical)
}
/// Model used in rendering the subtitle label.
@ -380,7 +383,7 @@ open class TitleLockup: View {
}
//pin the last view to the bottom of this view
previousView?.pinBottom(0, .defaultHigh)
previousView?.pinBottom(0)
//debugging for borders
eyebrowLabel.debugBorder(show: hasDebugBorder, color: .green)