perhapsmaybeharry
perhapsmaybeharry

Reputation: 894

What is the NSFont name for the font 'SF Mono'?

A new font titled 'SF Mono' was introduced with Xcode 8.

It's a font that I personally find very readable, and I would like to use it in a NSTextView. However, to set a font for an NSTextView, it is required to use an NSFont object. The problem is that I cannot seem to find the SF Mono font in the list of available fonts through NSFontManager.shared().availableFonts.

Does anyone know what the SF Mono's programmatic name is for the initialisation of an NSFont (NSFont(name: String, size: CGFloat)) for its typeface?

Upvotes: 10

Views: 7915

Answers (2)

alexkaessner
alexkaessner

Reputation: 2908

With iOS 13 and macOS 10.15 Apple made the new system fonts, including SF Mono and New York, available for developers.

You can get SF Mono using NSFont.monospacedSystemFont(ofSize: 0, weight: .regular).

Alternatively if you want to use the other system fonts you can use the NSFontDescriptor:

if let sfMonoDescriptor = NSFont.systemFont(ofSize: 0).fontDescriptor.withDesign(.monospaced) {
    let sfMonoFont = NSFont(descriptor: sfMonoDescriptor, size: 0)!
}

For more information look at the WWDC Session Font Management and Text Scaling.

Upvotes: 13

kennytm
kennytm

Reputation: 523314

"SF Mono" is not a system font. You cannot select SF Mono outside of Terminal, Console and Xcode because it is not installed to the system.

The "SF Mono" in Terminal is a custom font in /Applications/Utilities/Terminal.app/Contents/Resources/Fonts/.

Similarly, the "SF Mono" in Console is another custom font in /Applications/Utilities/Console.app/Contents/Resources/Fonts.

Similarly, the "SF Mono" in Xcode is also a custom font in /Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework/Resources/.

And of course you cannot bundle SF Mono alongside your program without violating Apple's copyright license:

This SF Mono Font (the “Apple Font”) is licensed to you by Apple Inc. (“Apple”) in consideration of your agreement to the following terms. If you do not agree with these terms, do not use the Apple Font.

You may use the Apple Font solely in conjunction with Apple-branded applications, including, but not limited to, Xcode, Terminal.app and Console.app. You may not embed or use the Apple Font in or with any other software applications or programs or other products and you may not use the Apple Font to create, develop, display or otherwise distribute any content, documentation, artwork or any other work product.

You may use the Apple Font only for the purposes described in this License or as otherwise expressly permitted by Apple in writing.

If the user has manually installed the font, it can be found with family name SF Mono, or the font name SFMono-Regular (similar for the other weights).

let f = NSFont(name: "SFMono-Regular", size: 12)

Upvotes: 17

Related Questions