diff --git a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h index af60fd34..8862221b 100644 --- a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h +++ b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h @@ -16,3 +16,4 @@ PUBLIC NSImageView* OakCreateDividerImageView (); PUBLIC NSBox* OakCreateViewWithColor (NSColor* color = nil, NSColor* secondaryColor = nil); PUBLIC NSBox* OakCreateVerticalLine (NSColor* primaryColor, NSColor* secondaryColor = nil); PUBLIC NSBox* OakCreateHorizontalLine (NSColor* primaryColor, NSColor* secondaryColor = nil); +PUBLIC BOOL OakSetAccessibilityLabel (NSObject* element, NSObject* label); diff --git a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm index 7ea5741c..ef632a84 100644 --- a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm +++ b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm @@ -211,3 +211,23 @@ NSImageView* OakCreateDividerImageView () [res setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationVertical]; return res; } + +BOOL OakSetAccessibilityLabel (NSObject* element, NSObject* label) +{ + if([element isKindOfClass:[NSControl class]]) + element = [(NSControl*)element cell] ?: element; // e.g. NSTableView is an NSControl with a nil cell + else if(!([element isKindOfClass:[NSView class]] || [element isKindOfClass:[NSCell class]])) + return NO; + + NSString* attribute = NSAccessibilityDescriptionAttribute; + if([label isKindOfClass:[NSView class]] || [label isKindOfClass:[NSCell class]]) + { + attribute = NSAccessibilityTitleUIElementAttribute; + if([label isKindOfClass:[NSControl class]]) + label = [(NSControl*)label cell] ?: label; + } + else if(![label isKindOfClass:[NSString class]]) + return NO; + + return [element accessibilitySetOverrideValue:label forAttribute:attribute]; +}