refactored for breadcrumbs wordwrap

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-05-29 15:17:07 -05:00
parent 756aa17e5d
commit 66a386a300
3 changed files with 71 additions and 95 deletions

View File

@ -14,77 +14,13 @@ final class BreadcrumbCellItem: UICollectionViewCell {
///Identifier for the BreadcrumbCellItem ///Identifier for the BreadcrumbCellItem
static let identifier: String = String(describing: BreadcrumbCellItem.self) static let identifier: String = String(describing: BreadcrumbCellItem.self)
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
internal var stackView: UIStackView = {
return UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.axis = .horizontal
$0.distribution = .fill
$0.alignment = .fill
$0.spacing = VDSLayout.space1X
$0.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
$0.setContentHuggingPriority(.defaultHigh, for: .horizontal)
}
}()
internal var breadCrumbItem: BreadcrumbItem?
///separator label
private var separator: Label = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .left
$0.numberOfLines = 1
$0.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
$0.setContentHuggingPriority(.defaultHigh, for: .horizontal)
$0.text = "/"
}
private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUp()
}
///Configuring the cell with default setup
private func setUp() {
separator.textColorConfiguration = textColorConfiguration.eraseToAnyColorable()
contentView.addSubview(stackView)
stackView.pinToSuperView()
separator.backgroundColor = .clear
}
///Updating the breadCrumbItem and UI based on the selected flag along with the surface ///Updating the breadCrumbItem and UI based on the selected flag along with the surface
func update(surface: Surface, hideSlash: Bool, breadCrumbItem: BreadcrumbItem) { func update(breadCrumbItem: BreadcrumbItem) {
//update surface contentView.subviews.forEach{$0.removeFromSuperview()}
separator.surface = surface contentView.addSubview(breadCrumbItem)
breadCrumbItem.surface = surface breadCrumbItem.pinToSuperView()
breadCrumbItem.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) breadCrumbItem.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
breadCrumbItem.setContentHuggingPriority(.defaultLow, for: .horizontal) breadCrumbItem.setContentHuggingPriority(.defaultLow, for: .horizontal)
//remove previous views
stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
//add to stack
stackView.addArrangedSubview(separator)
stackView.addArrangedSubview(breadCrumbItem)
stackView.setCustomSpacing(VDSLayout.space1X, after: separator)
//update separator
separator.textColor = textColorConfiguration.getColor(surface)
separator.isHidden = hideSlash
self.breadCrumbItem = breadCrumbItem
setNeedsLayout()
} }
} }

View File

@ -42,15 +42,21 @@ open class BreadcrumbItem: ButtonBase {
textColorConfiguration.getColor(self) textColorConfiguration.getColor(self)
} }
/// The natural size for the receiving view, considering only properties of the view itself. /// Determines if a slash is predended or not.
open override var intrinsicContentSize: CGSize { open var hideSlash: Bool = false { didSet { setNeedsUpdate() } }
guard let titleLabel else { return super.intrinsicContentSize }
// Calculate the titleLabel's intrinsic content size private var slashText = "/ "
let labelSize = titleLabel.sizeThatFits(CGSize(width: self.frame.width, height: CGFloat.greatestFiniteMagnitude))
// Adjust the size if needed (add any additional padding if your design requires) open override var textAttributes: [any LabelAttributeModel]? {
let adjustedSize = CGSize(width: labelSize.width + contentEdgeInsets.left + contentEdgeInsets.right, hideSlash
height: labelSize.height + contentEdgeInsets.top + contentEdgeInsets.bottom) ? nil
return adjustedSize : [ColorLabelAttribute(location: 0,
length: 1,
color: surface == .light ? VDSColor.elementsPrimaryOnlight : VDSColor.elementsPrimaryOndark),
TextStyleLabelAttribute(location: 0,
length: 1,
textStyle: .bodySmall)
]
} }
//-------------------------------------------------- //--------------------------------------------------
@ -68,15 +74,51 @@ open class BreadcrumbItem: ButtonBase {
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations. /// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
open override func setup() { open override func setup() {
super.setup() super.setup()
titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = .byWordWrapping
contentHorizontalAlignment = .left
isAccessibilityElement = true isAccessibilityElement = true
accessibilityTraits = .link accessibilityTraits = .link
contentHorizontalAlignment = .leading
} }
/// 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() {
//always call last so the label is rendered
super.updateView() //clear the arrays holding actions
accessibilityCustomActions = []
if let text, !text.isEmpty {
var updatedText = text
if updatedText.hasPrefix(slashText) && hideSlash {
updatedText = String(updatedText.dropFirst(slashText.count))
} else if !hideSlash, !updatedText.hasPrefix(slashText) {
updatedText = slashText + updatedText
}
//create the primary string
let mutableText = NSMutableAttributedString.mutableText(for: updatedText,
textStyle: textStyle,
useScaledFont: useScaledFont,
textColor: textColor,
alignment: titleLabel?.textAlignment ?? .center,
lineBreakMode: titleLabel?.lineBreakMode ?? .byTruncatingTail)
//apply any attributes
if let attributes = textAttributes {
mutableText.apply(attributes: attributes)
}
//set the attributed text
setAttributedTitle(mutableText, for: .normal)
setAttributedTitle(mutableText, for: .highlighted)
invalidateIntrinsicContentSize()
} else {
setAttributedTitle(nil, for: .normal)
setAttributedTitle(nil, for: .highlighted)
titleLabel?.text = nil
}
} }

View File

@ -32,13 +32,6 @@ open class Breadcrumbs: View {
} }
} }
/// Current Surface and this is used to pass down to child objects that implement Surfacable
override open var surface: Surface {
didSet {
breadcrumbs.forEach { $0.surface = surface }
}
}
open override var accessibilityElements: [Any]? { open override var accessibilityElements: [Any]? {
get { get {
return [containerView, breadcrumbs] return [containerView, breadcrumbs]
@ -165,17 +158,22 @@ extension Breadcrumbs: UICollectionViewDelegate, UICollectionViewDataSource, But
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BreadcrumbCellItem.identifier, for: indexPath) as? BreadcrumbCellItem else { return UICollectionViewCell() } guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BreadcrumbCellItem.identifier, for: indexPath) as? BreadcrumbCellItem else { return UICollectionViewCell() }
let hideSlash = indexPath.row == 0 let breadcrumb = breadcrumbs[indexPath.row]
cell.update(surface: surface, hideSlash: hideSlash, breadCrumbItem: breadcrumbs[indexPath.row]) breadcrumb.hideSlash = breadcrumb == breadcrumbs.first
breadcrumb.surface = surface
cell.update(breadCrumbItem: breadcrumb)
return cell return cell
} }
public func collectionView(_ collectionView: UICollectionView, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { public func collectionView(_ collectionView: UICollectionView, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let breadcrumb = breadcrumbs[indexPath.row] let breadcrumb = breadcrumbs[indexPath.row]
let intrinsicSize = breadcrumb.intrinsicContentSize breadcrumb.hideSlash = breadcrumb == breadcrumbs.first
let separatorFullWidth: CGFloat = indexPath.row == 0 ? 0 : VDSLayout.space1X + separatorWidth
let cellwidth = intrinsicSize.width + separatorFullWidth let maxWidth = frame.width
return .init(width: min(cellwidth, collectionView.frame.width), height: intrinsicSize.height) let intrinsicSize = breadcrumb.titleLabel!.sizeThatFits(.init(width: maxWidth, height: CGFloat.greatestFiniteMagnitude))
let cellwidth = min(maxWidth, intrinsicSize.width)
return .init(width: cellwidth, height: intrinsicSize.height)
} }
public func collectionView(_ collectionView: UICollectionView, buttonBaseAtIndexPath indexPath: IndexPath) -> ButtonBase { public func collectionView(_ collectionView: UICollectionView, buttonBaseAtIndexPath indexPath: IndexPath) -> ButtonBase {