Skip to content

Commit

Permalink
Support CommandBarButton with icon and label (#2117)
Browse files Browse the repository at this point in the history
  • Loading branch information
joannaquu authored Jan 16, 2025
1 parent d4b9283 commit 2b34d62
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class CommandBarButton: UIButton {
isAccessibilityElement = false
} else {
var buttonConfiguration = UIButton.Configuration.plain()
buttonConfiguration.title = item.title
buttonConfiguration.image = item.iconImage
buttonConfiguration.imagePadding = CommandBarTokenSet.buttonImagePadding
buttonConfiguration.contentInsets = CommandBarTokenSet.buttonContentInsets
buttonConfiguration.background.cornerRadius = 0
configuration = buttonConfiguration
Expand Down Expand Up @@ -87,14 +89,6 @@ class CommandBarButton: UIButton {
return
}

// always update icon and title as we only display one; we may alterenate between them, and the icon may also change
let iconImage = item.iconImage
let title = item.title
let accessibilityDescription = item.accessibilityLabel

configuration?.image = iconImage
configuration?.title = iconImage != nil ? nil : title

if let font = item.titleFont {
let attributeContainer = AttributeContainer([NSAttributedString.Key.font: font])
configuration?.attributedTitle?.setAttributes(attributeContainer)
Expand All @@ -105,7 +99,8 @@ class CommandBarButton: UIButton {

titleLabel?.isEnabled = isEnabled

accessibilityLabel = (accessibilityDescription != nil) ? accessibilityDescription : title
let accessibilityDescription = item.accessibilityLabel
accessibilityLabel = (accessibilityDescription != nil) ? accessibilityDescription : item.title
accessibilityHint = item.accessibilityHint
accessibilityValue = item.accessibilityValue
accessibilityIdentifier = item.accessibilityIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ open class CommandBarItem: NSObject {
}
}

/// Title for the item. Only valid when `iconImage` is `nil`.
/// Title for the item.
@objc public var title: String? {
didSet {
if title != oldValue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@ extension CommandBarTokenSet {
leading: 10.0,
bottom: 8.0,
trailing: 10.0)

/// The padding between the Command Bar Button image and title.
static let buttonImagePadding: CGFloat = GlobalTokens.spacing(.size60)
}

0 comments on commit 2b34d62

Please sign in to comment.