Skip to content

Commit 861794d

Browse files
Address review: named constant for accessibilityLabel suppression
Add AttributedLabel/Label.suppressedAccessibilityLabel so the "" suppression value is greppable and legible at the call site (per Rob's review), and rename the suppression test to flag that it guards an undocumented UIKit behavior. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 747f349 commit 861794d

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

BlueprintUICommonControls/Sources/AttributedLabel.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,15 @@ public struct AttributedLabel: Element, Hashable {
8080
/// Overrides the automatically-derived accessibility label.
8181
///
8282
/// When `nil` (the default), the label is derived from the displayed text — current behavior.
83-
/// Provide a string to override it, or `""` to suppress the spoken label entirely. Suppression
84-
/// is useful when the text is surfaced through `accessibilityValue` / `accessibilityHint`
85-
/// instead, so the content is not announced twice (e.g. when this label is merged into a
86-
/// combined accessibility element).
83+
/// Provide a string to override it, or `suppressedAccessibilityLabel` (`""`) to suppress the
84+
/// spoken label entirely. Suppression is useful when the text is surfaced through
85+
/// `accessibilityValue` / `accessibilityHint` instead, so the content is not announced twice
86+
/// (e.g. when this label is merged into a combined accessibility element).
8787
public var accessibilityLabel: String?
8888

89+
/// Assign to `accessibilityLabel` to suppress the spoken label entirely (see that property).
90+
public static let suppressedAccessibilityLabel = ""
91+
8992
/// A localized string that represents the current value of the accessibility element.
9093
///
9194
/// The value is a localized string that contains the current value of an element.

BlueprintUICommonControls/Sources/Label.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ public struct Label: ProxyElement {
6060
/// Overrides the automatically-derived accessibility label.
6161
///
6262
/// When `nil` (the default), the label is derived from the displayed text — current behavior.
63-
/// Provide a string to override it, or `""` to suppress the spoken label entirely. Suppression
64-
/// is useful when the text is surfaced through `accessibilityValue` / `accessibilityHint`
65-
/// instead, so the content is not announced twice (e.g. when this label is merged into a
66-
/// combined accessibility element).
63+
/// Provide a string to override it, or `suppressedAccessibilityLabel` (`""`) to suppress the
64+
/// spoken label entirely. Suppression is useful when the text is surfaced through
65+
/// `accessibilityValue` / `accessibilityHint` instead, so the content is not announced twice
66+
/// (e.g. when this label is merged into a combined accessibility element).
6767
public var accessibilityLabel: String?
6868

69+
/// Assign to `accessibilityLabel` to suppress the spoken label entirely (see that property).
70+
public static let suppressedAccessibilityLabel = ""
71+
6972
/// A localized string that represents the current value of the accessibility element.
7073
///
7174
/// The value is a localized string that contains the current value of an element.

BlueprintUICommonControls/Tests/Sources/AttributedLabelTests.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,14 @@ class AttributedLabelTests: XCTestCase {
115115
XCTAssertEqual(labelView.accessibilityLabel, "Custom")
116116
}
117117

118-
func test_accessibilityLabel_suppressed() {
118+
// Load-bearing: guards the undocumented UIKit behavior that `""`-suppression relies on.
119+
func test_accessibilityLabel_suppressed_guardsUIKitEmptyStringBehavior() {
119120
// An empty string suppresses the spoken label entirely: the backing view reports "",
120121
// *not* the displayed text. (UILabel returns an explicitly-set empty string verbatim,
121-
// rather than falling back to its text the way it does for `nil`.)
122+
// rather than falling back to its text the way it does for `nil`.) If a future OS changed
123+
// this fallback, `""`-suppression would silently break — hence this test.
122124
let label = AttributedLabel(attributedText: NSAttributedString(string: "Hello, World!")) {
123-
$0.accessibilityLabel = ""
125+
$0.accessibilityLabel = AttributedLabel.suppressedAccessibilityLabel
124126
}
125127

126128
let labelView = AttributedLabel.LabelView()
@@ -143,7 +145,7 @@ class AttributedLabelTests: XCTestCase {
143145
labelView.update(model: label, text: label.displayableAttributedText, environment: .empty, isMeasuring: false)
144146
XCTAssertEqual(labelView.accessibilityLabel, "Custom")
145147

146-
label.accessibilityLabel = ""
148+
label.accessibilityLabel = AttributedLabel.suppressedAccessibilityLabel
147149
labelView.update(model: label, text: label.displayableAttributedText, environment: .empty, isMeasuring: false)
148150
XCTAssertEqual(labelView.accessibilityLabel, "")
149151

@@ -158,7 +160,7 @@ class AttributedLabelTests: XCTestCase {
158160
// merged into a composite accessibility element — the empty label is filtered out, so the
159161
// content isn't announced twice.
160162
let label = AttributedLabel(attributedText: NSAttributedString(string: "Displayed text")) {
161-
$0.accessibilityLabel = ""
163+
$0.accessibilityLabel = AttributedLabel.suppressedAccessibilityLabel
162164
$0.accessibilityValue = "X"
163165
}
164166

0 commit comments

Comments
 (0)