Add accessibilityLabel override to AttributedLabel#625
Conversation
| /// is useful when the text is surfaced through `accessibilityValue` / `accessibilityHint` | ||
| /// instead, so the content is not announced twice (e.g. when this label is merged into a | ||
| /// combined accessibility element). | ||
| public var accessibilityLabel: String? |
There was a problem hiding this comment.
🤖 (on Rob's behalf) The String? shape is the right call here — it's consistent with the sibling accessibilityValue/accessibilityHint on this model, with AccessibilityElement.label, and with UIKit's own accessibilityLabel (nil = derive). And empty-string-means-absent is already the house convention: AccessibilityCombine documents that an element isn't accessible "unless it has a non-nil and non-empty label/value/hint," which is exactly why the composition test works. So the ""-suppresses behavior isn't a novel magic value — it fits existing precedent. I'd keep it over an enum, which would make this the only accessibility prop that isn't a plain optional string and would lose the x.accessibilityLabel = someOptionalString drop-in ergonomics.
One small discoverability nit: ""-as-suppression isn't legible at the call site, and a future reader could "fix" a bare "" thinking it's a mistake. Consider a named constant so intent is greppable:
extension AttributedLabel {
/// Assign to `accessibilityLabel` to suppress the spoken label entirely.
public static let suppressedAccessibilityLabel = ""
}Non-blocking — the doc comment already covers it well.
|
|
||
| func test_accessibilityLabel_suppressed() { | ||
| // An empty string suppresses the spoken label entirely: the backing view reports "", | ||
| // *not* the displayed text. (UILabel returns an explicitly-set empty string verbatim, |
There was a problem hiding this comment.
🤖 (on Rob's behalf) Good that this is called out inline — the whole suppression story rests on this undocumented UIKit behavior (an explicitly-set "" is returned verbatim rather than falling back to .text). That makes this test load-bearing rather than incidental: if a future OS changed the fallback, ""-suppression would silently break. Worth keeping and maybe a one-liner in the test name/doc flagging it as guarding a UIKit assumption.
861794d to
2020962
Compare
Adds an optional `accessibilityLabel: String?` to the AttributedLabel model: `nil` (default) derives the label from the displayed text as before, a non-nil value overrides it, and `""` suppresses the spoken label entirely (useful when content is surfaced via accessibilityValue and the label is merged into a combined accessibility element). The expensive text-derived label is now cached behind the existing text-change guard in LabelView, while the override is applied on every update so a change takes effect even when the text is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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>
2020962 to
d0a313d
Compare
Adds an optional
accessibilityLabel: String?to theAttributedLabelmodel so consumers can override or suppress the label that is otherwise derived automatically from the displayed text.nil(the default) preserves today's text-derived behavior, a non-nil value overrides it, and""suppresses the spoken label entirely — useful when the text is surfaced throughaccessibilityValue/accessibilityHintand the label is merged into a combined accessibility element, so the content isn't announced twice. The change is purely additive: no existing call sites change. Internally, the expensive text-derived label is cached behind the existing text-change guard inLabelViewwhile the override is applied on every update, so an override change takes effect even when the text is unchanged, with no perf regression. Adds tests covering the default/override/suppression cases, application without a text change, and suppression flowing throughAccessibilityComposition.