SwiftUI: custom Font on MacOS
Asked Answered
P

2

6

Goal: use a custom Font on SwiftUI, targeting MacOS.

Problem: On iOS, custom Font works fine in SwiftUI: enter image description here

But on MacOS, it doesn't: enter image description here

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
                .font(Font.custom("SourceCodePro-ExtraLight", size: 40))
            Text("Hello, world!")
                .font(Font.custom("LobsterTwo", size: 40))

        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Tried: I made sure that both fonts are added to Info tab on corresponding iOS and MacOs targets: enter image description here enter image description here

Seems to be a problem with SwiftUI using UIFont under the hood, and a special NSFont would be needed...

Any help is much appreciated!

Patina answered 22/11, 2022 at 16:42 Comment(0)
P
8

enter image description here

Solved by adding this line to the plist file, and a "." as the value

Patina answered 22/11, 2022 at 17:44 Comment(2)
I <3 you @mane! I have been fighting this for hours. Everything tells me to update the info.plist file directly (etc etc). No change. This image above was the first to indicate that this value can be changed/added in PROJECT/TARGET/INFO settings! FTWNic
Can confirm this works on macOS finally ! ThanksMoonstone
T
0

Just some further explanation from Mane Manero's answer:

The Info.plist key ATSApplicationFontsPath actually refers to the application bundle’s Resources folder, as opposed to your Project directory. Here is the specific reference from the docs:

If you set this key, the system allows the app in the bundle to use the fonts at the specified path. Set this key to the path relative to the bundle’s Resources folder. For example, if the fonts are in .../Resources/MyFonts, set this key to MyFonts/.

You can confirm this by archiving and exporting your app in Xcode -> save app bundle to Applications folder -> right click app bundle > Show Package Contents > open Resources folder -> You should see your .ttf or .otf font files in this directory.

Toxemia answered 10/9 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.