his*_*aac 1 macos accessibility appkit voiceover swift
我正在努力为现有 macOS 代码库中的自定义 UI 元素添加可访问性。我们有一个自定义错误窗口,它会在发生错误时出现,我正在尝试让 VoiceOver 在发生错误时宣布错误。
目前,我正在尝试使用该NSAccessibility.post(element: Any, notification: NSAccessibility.Notification, userInfo: [NSAccessibility.NotificationUserInfoKey : Any]?)方法,但是在运行代码时,没有说出通知。
这是我目前在 NSViewController 中的内容:
NSAccessibility.post(element: self, notification: .announcementRequested, userInfo: [
.announcement: NSLocalizedString("CANT_CONNECT_ERROR", comment: "Error string for connection failure"),
.priority: NSAccessibilityPriorityLevel.high
])
Run Code Online (Sandbox Code Playgroud)
我的期望是,使用该.announcementRequested选项,VoiceOver 应该会自动选择并说出公告,但没有运气。我尝试使用不同的通知类型,尝试在.announcement参数中放入一个硬编码的字符串值,并尝试使用.rawValue和90作为.priority参数。
因此,经过多次反复试验(以及danielpunkass的有用评论),我终于想出了如何使其工作。YMMV,但这是我让它工作的方式:
element参数必须设置为NSApp.mainWindow(我也添加了as Any以消除编译器警告)userInfo字典中的优先级必须使用 设置.rawValue,而不是实际的枚举值。苹果的文档当然没有提到这一点。这是我的工作代码:
NSAccessibility.post(
element: NSApp.mainWindow as Any,
notification: .announcementRequested,
userInfo: [
.announcement: "This is a custom accessibility notification",
.priority: NSAccessibilityPriorityLevel.high.rawValue
]
)
Run Code Online (Sandbox Code Playgroud)