Accessibility live region - SwiftUI

In SwiftUI, the closest thing to live regions are elements with the updatesFrequently trait. When an element is focused, label and value changes are announced periodically.

You can also replicate a live region by posting accessibility announcements. To replicate 'polite' behavior, you can set accessibilitySpeechQueueAnnouncement to false. To be 'assertive', set the value to true.

For even more advanced behavior, you can act on announcementDidFinishNotification events.

WARNING: You likely should not mix updatesFrequently trait and manual announcements. Choose one approach.

@State var stockPrice = "Stock Price: $123.45"

var body: some View {
    // Periodic announcements (only on focus!)
    Text(stockPrice)
        // Add the updatesFrequently trait here
        .accessibilityAddTraits(.updatesFrequently)
        .onChange(of: stockPrice) { _, newPrice in
            // Replicate live region
            let message = NSAttributedString(
                string: newPrice,
                attributes: [.accessibilitySpeechQueueAnnouncement: true]
            )
            UIAccessibility.post(notification: .announcement, argument: message)
        }
}