Repository navigation
a11y(swiftui): announce visible text changes in live regions - #4021
Conversation
A live component announced only its explicit accessibility label, so a live Text or a live Column/Card banner without a label stayed silent when its text changed. Components now report their visible text through A2UIAccessibleTextKey (.a2uiAccessibleText(_:)). A2UIText reports its text without Markdown syntax. A live, non-hidden node announces its explicit label if set, otherwise the joined subtree text, whenever it changes. Hidden subtrees report no text, and a live region doesn't pass its text to ancestors, so nested live regions announce once. Part 1 of a2ui-project#3147.
…bers Components now build and report their visible text only inside a live region, so surfaces without one don't parse Markdown a second time. Ordered list items keep their numbers in the announced text.
A list item with several paragraphs repeated its number before each one. Number only the item's first block, as the screen shows it.
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism for SwiftUI components to report their visible text to enclosing live regions using a custom preference key (A2UIAccessibleTextKey) and environment values. This allows live regions to announce updates to their visible text dynamically without needing to read catalog properties directly. The changes include helper modifiers, Markdown text parsing for accessibility, and comprehensive unit tests to verify the behavior. There are no review comments to address, and the implementation looks solid.
…le text extension a2ui-project#3887 made it private, so A2UIText+AccessibleText.swift no longer compiled after the merge from main.
…unted when hidden - A live region announces its explicit label followed by its subtree text. A label on a `.combine` element replaces the visible text it reports, and a labeled component inside a live region reports its label. - `hidden` no longer swaps the view tree, so a bound `hidden` keeps component state and showing a live region again announces its text. A live region under a hidden ancestor announces nothing. - Add an `@_spi(Testing)` `a2uiAnnouncer` environment value, and test the posted announcements. LiveRegionTextTests now tear down their window.
…za/a2ui into a11y/swiftui-live-region-text
|
Thanks for the clean updates — Left one blocking catch in Could you also:
|
…Button's label Apply accessibilityHidden(true) only when hidden is set. On iOS 18 and macOS 15 use accessibilityHidden(_:isEnabled:) so a bound hidden keeps the view tree. A Button with an explicit label no longer reports its content text to live regions, and labelText reports through a2uiAccessibleText(nil) instead of switching branches.
|
Thanks for the second pass. The three inline notes are addressed in b87a5b9. In b3b4079 I added a |
Description
Part of #3147 (part 1, live regions).
In SwiftUI, a component with
accessibility.liveset topoliteorassertiveonly announced itsexplicit
accessibility.label. A live statusText, or a liveColumn/Cardbanner without alabel, said nothing when its text changed.
Following the design in #3147:
A2UIAccessibleTextKey(inA2UISwiftUI) collects visible text in tree order. Components reportit with the new
.a2uiAccessibleText(_:)modifier.A2UITextreports its text without Markdown syntax. Block Markdown is joined with line breaks,and ordered list items keep their numbers. Text is only built inside a live region, so surfaces
without one do no extra work.
A2UIText.swiftadds one line and makesattributedMarkdown(_:)internal again, so the extension can use it.
A2UIAccessibilityModifierreads the subtree text on a live node and, when it changes,announces the explicit label followed by the joined text. It no longer depends on any catalog
property names.
.combineelement (such asText) or on aButtonreplaces its visible text forVoiceOver, so that text isn't reported and the label is reported instead. A labeled live
Textreads its label once, and a labeled live
CardorColumnreads its label and the updated text.hiddenonly addsaccessibilityHidden(true)when set, so a hidden child of a visiblecontainer stays hidden. On iOS 18 and macOS 15 it uses
accessibilityHidden(_:isEnabled:), soa bound
hiddenkeeps the component's state. Showing a live region again announces its text.A live region doesn't pass its text to its ancestors, so a nested live region announces an
update once.
Not in this PR: part 2 of #3147 (
descriptionon.containgroups). OnlyTextreports text fornow;
TextField,CheckBox,SliderandChoicePickercould follow.Testing:
LiveRegionTextTestshost a surface inside a live region and check the reported text:inline and block Markdown (including a multi-paragraph ordered list item), tree order, labels,
a
Buttonlabel replacing its content, hidden and nested live subtrees, a data model update,and nothing reported outside a live region. Unit tests cover the announced text (label
first, joined text,
live: off, hidden).AccessibilityModifierTestschecks the native AppKit accessibility tree: ahiddenchild of avisible
Columnis not in it.@_spi(Testing)a2uiAnnouncerenvironment value lets the tests record the postedannouncements: nothing on mount, one assertive announcement per data model update, a labeled
live
Textread once, showing a hidden live region, and silence under a hidden ancestor.swift test --enable-swift-testing --no-parallel: 967 tests pass.xcodebuild buildofA2UISwiftCore-Packageand the sample app for the iOS Simulator.xcrun swift-format lint --stricton the changed files: clean.Pre-launch Checklist
One time:
For this PR:
swift/swiftui/README.md.)If you need help, consider asking for advice on the discussion board.