如何设置NSTableView列以使用等宽数字?

问题描述:

El Capitan引入了San Francisco系统字体,默认情况下该字体具有比例数字。

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:

我要启用固定宽度数字选项

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.

在Interface Builder中,选择字体>字体面板>版式>等宽数字,然后使用默认系统字体并保持与OS X早期版本的向后兼容性。 不会影响字体(XIB文件保持不变)。

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

在OS X表格视图列中设置等宽数字的正确方法是什么? (我怀疑IB无法为此使用,因此也可以使用编程解决方案。)

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).

只需使用 + [NSFont monospacedDigitSystemFontOfSize:weight:] (如果可用)。它是10.11中的新功能,但仍未在 NSFont 文档中使用。它在标题中,并在WWDC 2015视频中进行了讨论。因此,类似于:

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];