问题
I'm trying to hide several elements in my app from VoiceOver so that they don't get read aloud by the screen reader. On iOS, I'd set isAccessibilityElement
to NO
, but this has no effect on OSX. What's the correct way to go about hiding elements from VoiceOver?
For example, I have a series of labels contained inside a view that make no sense if they're spoken separately by VoiceOver. I'd like to set the accessibilityLabel
on the container view to describe all the labels nested within. But if I do that, the labels inside are still read out by VoiceOver.
回答1:
In macOS, it is true that setting accessibilityElement
to NO
for NSButton
, NSTextField
and NSImageView
has no effect. That is because these are controls – they inherit from NSControl
. To make it work for controls, you must do it instead to the control's cell.
In an Objective-C project, I so subclassed several Cocoa controls. For example, whenever I want a image view to be skipped by VoiceOver, I set its Custom Class in Interface Builder to this:
/*!
@brief Image view which will be skipped over by VoiceOver
@details Be careful that you *really* want the view to be skipped over by
VoiceOver, because its meaning is conveyed in a better, non-visual way,
elsewhere. Remember that not all VoiceOver users are completely blind.
*/
@interface SSYNoVoiceOverImageView : NSImageView {}
@end
@implementation SSYNoVoiceOverImageView
- (void)awakeFromNib {
self.cell.accessibilityElement = NO;
}
@end
回答2:
If you set an element's accessibility role to an empty string, Voice Over won't detect it. I had to hide some NSImageView elements in my app because their file names were being read out and it was confusing for the VO user.
Either
[element accessibilitySetOverrideValue:@"" forAttribute:NSAccessibilityRoleAttribute];
or else
[[element cell] accessibilitySetOverrideValue:@"" forAttribute:NSAccessibilityRoleAttribute];
should do the trick.
I know that Apple a new method based Accessibility API but it only works for OS X 10.10 onwards and the application I'm working needs to be compatible with 10.9.
If you can use the new API
[element setAccessibilityRole:@""];
or [[element cell] setAccessibilityRole:@""];
should do the same thing.
来源:https://stackoverflow.com/questions/31523333/disable-hide-accessibility-element