How can I set the NSTableView column to use monospace numbers?

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

This leads to the fact that the numbers in the columns of the table look uneven and difficult to compare:

all 6 digit numbers

I would like to enable the fixed font width option for the font, but continue to use the system default font and maintain backward compatibility with earlier versions of OS X.

In the Builder interface, select Font> Font Panel> Typography> Monospaced numbers do not affect the font (the XIB file remains unchanged).

useless panel

What is the correct way to set monospace numbers in the columns of a table in an OS X table? (I suspect IB is not suitable for this, so the software solution is also OK).

+7
fonts cocoa osx-elcapitan nstableview
source share
4 answers

Just use +[NSFont monospacedDigitSystemFontOfSize:weight:] when available. This is new in 10.11, but still not in NSFont . This is in the headlines and was discussed in the WWDC 2015 video. So, something like:

 if ([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) textField.font = [NSFont monospacedDigitSystemFontOfSize:textField.font.pointSize weight:NSFontWeightRegular]; 
+8
source share

Consider the following as pseudo-code quickly made, not verified, etc.

Given NSFont , which represents a font that has monospace numbers as a function, the following method will produce another NSFont with the function selected:

 - (NSFont *) newMonospaceNumbersFont:(NSFont *)font { CTFontDescriptorRef origDesc = CTFontCopyFontDescriptor((__bridge CTFontRef)font); CTFontDescriptorRef monoDesc = CTFontDescriptorCreateCopyWithFeature(origDesc, (__bridge CFNumberRef)@(kNumberSpacingType), (__bridge CFNumberRef)@(kMonospacedNumbersSelector)); CFRelease(origDesc); CTFontRef monoFont = CTFontCreateWithFontDescriptor(monoDesc, font.pointSize, NULL); CFRelease(monoDesc); return (__bridge_transfer NSFont *)monoFont; } 

You can use this, say, to take the current font of a user interface element and convert it to one with monospace numbers.

NTN

Option for Swift

Assuming res is an NSTextField with a number to display:

 let origDesc = CTFontCopyFontDescriptor(res.font!) let monoDesc = CTFontDescriptorCreateCopyWithFeature(origDesc, kNumberSpacingType, kMonospacedNumbersSelector) let monoFont = CTFontCreateWithFontDescriptor(monoDesc, res.font!.pointSize, nil) res.font = monoFont 
+3
source share

In my experience, the functionality of the “font panel” is not completely defined, and I usually just ignore it whenever I mess with XIB or Storyboard.

What should work, let's go back to this “Font” attribute in the attribute inspector of the text field cell, and then select “Custom fixed step” in the “Font” drop-down menu (the choice should automatically be set to size 11 by default).

Choose your font this way

If you reduce the font size to a dot, it will magically switch to Monaco (fixed-width font by default).

+2
source share

Here's a speed extension that gives you a monospaced font with high definition.

 extension NSFont { var legibleNumbersVariant: NSFont { let features = [ [NSFontFeatureTypeIdentifierKey: kNumberSpacingType, NSFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector], [NSFontFeatureTypeIdentifierKey: kStylisticAlternativesType, NSFontFeatureSelectorIdentifierKey: kStylisticAltSixOnSelector] ] let descriptor = fontDescriptor.addingAttributes([NSFontFeatureSettingsAttribute: features]) return NSFont(descriptor: descriptor, size: pointSize) ?? self } } 

Comparison

+1
source share

All Articles