Search code examples
macosaccessibilitynsbuttonvoiceover

How do I change what is read by VoiceOver for an NSButton that has no Title (image only)?


I have a few NSButtons that are images only, and are set completely programmatically. When VoiceOver reads these buttons, it says "unchecked checkbox" for each of them. I'd like to set an attribute for VoiceOver to read that is more descriptive (for accessibility purposes), but I can't set the title attribute on the button because the title will then be displayed (I only want the icon to be displayed).

I tried using

[self.button accessibilitySetOverrideValue:title forAttribute:NSAccessibilityDescriptionAttribute];

but nothing changed. I also tried NSAccessibilityTitleAttribute, NSAccessibilityRoleDescriptionAttribute, and NSAccessibilityRoleAttribute with no luck. Am I doing something incorrectly?

Thanks.


Solution

  • It looks like the original code was correct, but it only works when called on the button cell (not the button itself). The above was changed to:

    [self.button.cell accessibilitySetOverrideValue:title forAttribute:NSAccessibilityDescriptionAttribute];
    

    where title is still an NSString*. Assuming title is @"Test", the above code causes VoiceOver to read "Test unchecked checkbox." The only way I found to have it not read the "unchecked checkbox" parts was to clear three other attributes: NSAccessibilityTitleAttribute, NSAccessibilityRoleDescriptionAttribute, and NSAccessibilityRoleAttribute. I overrode each of these with @" ". If anyone has a more elegant solution to ignore these fields, please let me know. For now, I'll mark this answered.