Search code examples
macoscocoansbutton

Default NSRecessedBezelStyle NSButton visual bug?


I have a basic NSRecessedBezelStyle NSButton added via IB to an NSView. Why is the font messed up in its unselected state? Is this normal?

As you can see, when pushed the recessed button looks fine, but unpressed it's solid black with no shadow. Am I missing something really obvious somewhere? I tried messing around with setAttributedTitle and setAttributedAlternateTitle but that yielded odd results with the push on push off mechanic.


Solution

  • That is the expected behavior for NSRecessedBezelStyle with the default "Push On Push Off" Type, bezeled in On state, plain text in OFF, additionally you can change the Type so the bezel is only displayed when hovering, here is the code to make it gray.

    NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithCapacity:1];                
    [attrsDictionary setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
    [attrsDictionary setObject:[NSFont boldSystemFontOfSize:12.0] forKey:NSFontAttributeName];      
    NSMutableParagraphStyle *paragraph = [[[NSMutableParagraphStyle alloc] init] autorelease];
    [paragraph setAlignment:NSCenterTextAlignment];
    [attrsDictionary setObject:paragraph forKey:NSParagraphStyleAttributeName];    
    NSAttributedString *str = [[[NSAttributedString alloc] initWithString:@"Button" attributes:attrsDictionary] autorelease];
    [button setAttributedTitle:str];