Skip to content

Commit e596085

Browse files
authored
Add the Ability to Truncate Long Labels in the Command Bar (#2259)
* Made the CommandBar label truncate after they reach a specified length if the Command Bar is not long enough to scroll * Fixed truncation issues * Simplified logic to avoid trying to fill space before truncating. We now always truncate * Provided obj c exposure * Moved max button width to a token * Removed the buttonWidth exposure from the CommandBar class * Simplified constraints to use tokens * Removed unwanted whitespace
1 parent 02f669e commit e596085

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

Sources/FluentUI_iOS/Components/CommandBar/CommandBarButton.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class CommandBarButton: UIButton {
5454
buttonConfiguration.imagePadding = CommandBarTokenSet.buttonImagePadding
5555
buttonConfiguration.contentInsets = CommandBarTokenSet.buttonContentInsets
5656
buttonConfiguration.background.cornerRadius = tokenSet[.cornerRadius].float
57+
buttonConfiguration.titleLineBreakMode = .byTruncatingMiddle
5758
configuration = buttonConfiguration
5859

5960
let accessibilityDescription = item.accessibilityLabel
@@ -97,6 +98,11 @@ class CommandBarButton: UIButton {
9798
return
9899
}
99100

101+
let maxWidth: CGFloat = tokenSet[.maxButtonWidth].float
102+
if maxWidth > 0 {
103+
widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth).isActive = true
104+
}
105+
100106
configuration?.title = item.title
101107
configuration?.image = item.iconImage
102108

Sources/FluentUI_iOS/Components/CommandBar/CommandBarTokenSet.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public enum CommandBarToken: Int, TokenSetKey {
5050

5151
/// The shadows used by the `CommandBar`.
5252
case shadow
53+
54+
/// The maximum width of each button in the CommandBar. When greater than 0, buttons will be constrained to this width and truncate text that exceeds it.
55+
case maxButtonWidth
5356
}
5457

5558
/// Design token set for the `CommandBar` control.
@@ -98,6 +101,9 @@ public class CommandBarTokenSet: ControlTokenSet<CommandBarToken> {
98101

99102
case .shadow:
100103
return .shadowInfo { theme.shadow(.shadow08) }
104+
105+
case .maxButtonWidth:
106+
return .float { 0.0 }
101107
}
102108
}
103109
}

0 commit comments

Comments
 (0)