The NSFont
API has two different ways to specify a weight. First, there is a struct NSFont.Weight
which contains a floating point rawValue
property. It's used in functions like:
NSFont.systemFont(ofSize fontSize: CGFloat, weight: NSFont.Weight) -> NSFont
Then there is another function for getting other fonts, which uses an integer.
NSFontManager.font(withFamily family: String, traits: NSFontTraitMask,
weight: Int, size: CGFloat) -> NSFont?
The documentation for that function says that integers are not simply rounded versions of the floats. The weight
can be in the range 0-15, with 5 being a normal weight. But:
NSFont.Weight.regular.rawValue == 0.0
NSFont.Weight.light.rawValue == -0.4000000059604645
NSFont.Weight.black.rawValue == 0.6200000047683716
I don't see any mention of how to convert between the NSFont.Weight
and the integers. Maybe it's just some odd legacy API that they never clean up. Am I missing something?
Here's the mapping I came up with (by writing an RTF and looking at it in TextEdit's font panel), as of 13.4 Ventura:
NSFont.systemFont(ofSize: 14, weight…)
calls yield (all Helvetica Neue):NSFontManager.shared.font(…weight: intWeight…)
calls yield:Ergo: