swiftswiftuiappkitnsfont

List all available fonts in SwiftUI for macOS


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


Solution

  • 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))
            }
        }
    }