0

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?

phimuemue
  • 34,669
  • 9
  • 84
  • 115

1 Answers1

1

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.

Keith Knauber
  • 752
  • 6
  • 13
  • Thanks for the suggestion. I stepped back from using `NSAttributedString` entirely and now go with just `setFont`, which seems to work fine so far. – phimuemue Oct 18 '18 at 07:29