List all available fonts in SwiftUI for macOS

558 Views Asked by At

I know how to do this for iOS but I can't figure out how to do it for macOS.

let allFontNames = UIFont.familyNames.flatMap { UIFont.fontNames(forFamilyName: $0) }

 var body: some View {
   List(allFontNames, id: \.self) { name in
     Text(name)
       .font(Font.custom(name, size: 12))
   }
 }

Source for iOS code

1

There are 1 best solutions below

0
AudioBubble On BEST ANSWER

Here is an example using NSFontManager:

struct FontView: View {
    let allFontNames = NSFontManager.shared.availableFonts
    var body: some View {
        List(allFontNames, id: \.self) { name in
            Text(name).font(Font.custom(name, size: 12))
        }
    }
}