I'm writing a multiplatform SwiftUI app, and I'd like to use accessibilityName to get human-readable names of colours. On iOS this works:
let myColor = Color.red
return UIColor(myColor).accessibilityName
and with arbitrary user-entered colours I get nice descriptions, such as 'dark blue', 'dark pink red' etc.
But for macOS, if I try:
return NSColor(myColor).accessibilityName
I get the error:
Value of type 'NSColor' has no member 'accessibilityName'
I see in NSAccessibilityColor.h that NSColor should have this accessibilityName property from macOS 11 and up:
API_AVAILABLE(macos(11.0)) @protocol NSAccessibilityColor
@required
/*!
@brief Returns a localized description of the color for use in accessibility attributes.
*/
@property(readonly) NSString *accessibilityName;
@end
@interface NSColor (NSAccessibilityColorConformance) <NSAccessibilityColor>
@end
I am targeting macOS 12.3. So how come I can't access this property? I also tried casting the NSColor to NSAccessibilityColor, but I get:
Cannot find type 'NSAccessibilityColor' in scope
So it seems that whole header is ignored. Do I need to add a bridging header to get NSAccessibilityColor.h, even though I'm already importing AppKit?
The best alternative I've found (other than writing my own logic to create a name from the RGB values, which would be quite a sidetrack given the functionality already exists) is to use NSColor.description, but for arbitrary colours this just gives hex values, which is not particularly useful for accessibility.