phimuemue
phimuemue

Reputation: 36031

NSButton attributedTitle changes color on button down

I created a Cocoa app with a button on it, having the "Check" style and "Switch" type. It is set to a custom class MyButton:

@interface MyButton : NSButton
- (void)awakeFromNib;
@end

This custom class sets attributedTitle:

@implementation MyButton
- (void)awakeFromNib {
    [super awakeFromNib];       
    [self setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Hallo" attributes:@{
    }]];
}
@end

When the button is checked, the font color changes on mouse-down.

I read that NSAttributedString's default color is black, but when I set it to black explicitly, it stays black on mouse-down. If I explicitly ask for [NSColor controlTextColor], the color switches on mouse-down. Is this behaviour documented somewhere? Is it intended or a bug?

Upvotes: 0

Views: 313

Answers (1)

Keith Knauber
Keith Knauber

Reputation: 802

In your case, you may want to subclass NSButtonCell and override some methods.

One possible avenue is overriding NSCell's method

- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView

This is one of many frustrating button drawing problems. After trying to bend NSButtonCell drawing to my needs, I ended up drawing everything myself. I started with the ancient but awesome BGHUDAppKit, and heavily modded it to fit my needs. BGHUDAppKit is monstrous, but at least now I can easily debug or workaround most drawing bugs. I feel for all the people that will encounter bugs similar to this as they try to support Mojave dark mode.

Upvotes: 1

Related Questions