cocoaappkitnsfont

Fixed spacing numbers in AppKit


I have an app with a timer. This timer is centered in the view. When macOS's default font was Helvetica Neue, this worked fine. It seems that the font's numbers are spaced evenly, so when the timer is running, everything stays in place.

Now with San Francisco as the default font, this is broken. Ever number seems to have a different width, causing the timer to flux in size continuously while running. Hard coding the font to Helvetica Neue fixes the issue, but I'd like to use the font that's default to the OS.

Is there some kind of hint I can set with NSTextField that would render the default system font with fixed width numbers like before? I seem to remember this being available, but I cannot find it. I don't want to use a fixed-width font, as it breaks the aesthetic. It seems like the stop watch app on iOS is doing this somehow.


Solution

  • Use this class method of NSFont

    Available macOS 10.11+