- [NSString drawInRect:withAttributes:]; is buggy. Is there a workaround?

Context:


Dark Mode support in Cocoa is still not as good as it should be in macOS 10.14.2.


Problem:


Let's say I have a window "Window" with some subviews including a custom view "View".


"View" uses - [NSString drawInRect:withAttributes:] to draw some strings within the drawRect: method. The NSForegroundColorAttributeName attribute is set to [NSColor tertiaryLabelColor]


State A:


"Window" effective appearance is the current OS appearance.

"View" effective appearance is the current OS appearance.


"View" is drawn correctly.


State B:


Now let's set "View" effective appearance to the opposite appearance of the current OS appearance (Light when Dark or Dark when Light).


Now, "View" is drawn incorrectly. The alpha channel is not correctly taken into account from what I'm observing.


Questions:


. Is there a workaround that does not involve embedding a NSTextField (or NSTextFieldCell)?

. Is there any hope for Cocoa to work correctly with Dark Mode before 2030?

>>"View" uses - [NSString drawInRect:withAttributes:] to draw some strings within the drawRect: method.

>>The NSForegroundColorAttributeName attribute is set to [NSColor tertiaryLabelColor]


Sorry if this is a stupid question, but what are you setting the background color to? What happens if you set it to an "adaptable" color such as the control background color or the text background color?


https://developer.apple.com/documentation/appkit/nscolor/ui_element_colors

Not a stupid question IMO.


The view hierarchy from the first opaque view to "View" is:


- NSView subclass (not opaque) that fills most of its frame with NSDrawNinePartImage using opaque NSImage instances and the NSCompositeSourceOver option and with alpha = 1.0.


- NSImageView subclass (drawRect is overridden to clip NSImageView drawing but it does not impact "View" frame).


- "View" (Not opaque, no background being drawn)



"View" is basically drawn over the result of NSDrawNinePartImage which is opaque.


The NSString is drawn with no background color set.

- [NSString drawInRect:withAttributes:]; is buggy. Is there a workaround?
 
 
Q