Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

nsfont

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?

like image 235
perhapsmaybeharry Avatar asked Oct 06 '16 08:10

perhapsmaybeharry


1 Answers

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.

like image 141
alexkaessner Avatar answered Sep 18 '22 13:09

alexkaessner