How to make NSButton to use subpixel antialiasing / by Brian Lambert

For some reason NSButton doesn't use subpixel antialiasing for its label like other AppKit controls such as NSSegmentedControl.

Here's an example:

I noticed this right away and I found it really annoying. 

In order to fix this in my work, I created a subclass of NSButton which overrides the drawRect method and enables subpixel antialiasing for drawing the label.

Here's the code:

// Draw the receiver’s image within the specified rectangle.
- (void)drawRect:(NSRect)rect
    // Get the graphics context.
    CGContextRef context = [[NSGraphicsContext currentContextCGContext];
    // Save the graphics context state.
    // Set up font rasterization so that subpixel antialiasing is used.
    CGContextSetShouldSmoothFonts(context, TRUE);
    CGContextSetShouldSubpixelPositionFonts(context, TRUE);
    CGContextSetShouldSubpixelQuantizeFonts(context, TRUE);
    // Draw the button.
    [super drawRect:rect];
    // Restore the graphics context state.

The result can be seen below. Both buttons were created using Interface Builder, but the top button has its class set to a subclass with the drawRect override. The bottom button does not. The improvement is easily noticeable:

Curiously, Apple seems to have done something like this for its own applications. I checked out buttons in System Preferences, and their labels are drawn with subpixel antialiasing. I wonder why this isn't the case for apps that people make using Interface Builder? Odd...