Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set NSTableView column to use monospaced numbers?

El Capitan introduced San Francisco system font, which has proportional digits by default.

This makes numbers in table columns look jagged and hard to compare:

all are 6-digit numbers

I'd like to enable fixed-width numbers option for the font, but keep using the default system font and keep backwards compatibility with earlier versions of OS X.

In Interface Builder selecting font > Font Panel > Typography > Monospaced Numbers does not affect the font (XIB file remains unchanged).

useless panel

What's the right way to set monospaced numbers in OS X table view columns? (I suspect IB is unusable for this, so a programmatic solution is OK too).

like image 405
Kornel Avatar asked Nov 27 '15 09:11

Kornel


1 Answers

Just use +[NSFont monospacedDigitSystemFontOfSize:weight:] when it's available. It's new in 10.11, but still not in the NSFont docs. It's in the headers and was discussed in the WWDC 2015 videos. So, something like:

if ([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)])
    textField.font = [NSFont monospacedDigitSystemFontOfSize:textField.font.pointSize weight:NSFontWeightRegular];
like image 119
Ken Thomases Avatar answered Sep 23 '22 00:09

Ken Thomases