TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to use custom fonts in Swift Playgrounds 4.2.1 (.swiftpm app playground project)?

Forums > SwiftUI

I have done plenty of research, and I am aware of this post on the same exact question, and I tried that exact solution. However, it will not work as it throws the fatal error, "Couldn't create font from data". It seems like Swift cannot find my font.

I am currently using Xcode to create a SwiftUI project in an app playground (.swiftpm) file. This file can then be moved over to the MacOS Playgrounds app or iPadOS Playgrounds app etc. I found a custom .otf font that I want to use for my project, but I am having trouble using it.

If you have any experience with these, you would know that .swiftpm Playgrounds lack Info.plist files, so you cannot simply do what you would do in an Xcode project to use custom fonts (which involve a .plist).

How can I do this? I would really like to have a solution in SwiftUI, and I really wished that the linked solution would work as it seems like a great solution. My only thought now is that I am putting the fonts in the wrong directory, as .swiftpm files do not come with a Resources folder, unlike an older macOS .playground file, for example. I will attach a picture of my file structure here if that is helpful. I definitely know that the official PostScript name of my files match the names of the font variations in the screenshot (I double checked in Mac's FontBook). I have been trying to fix this for long enough to know that I definitely am not spelling anything wrong and I know that the file is definitely not corrupt because I can use it in other softwares and I got it from a reliable source.

If anyone knows how to do this, I would really appreciate your help. Feel free to look at the previous solution I was looking at linked above. Thanks! :)

As an additional note, the Font.swift file is completely empty, so you aren't missing anything from not seeing that. And yes, I have tried moving where the fonts are to many, many places, including the Assets.xcassets folder. Nothing has worked so far.

View photo here (Sorry, I can't add direct photos in here): https://developer.apple.com/forums/thread/727740

Yes, I posted this on the Apple Developer Forums as well but unforuntely there aren't many people that tend to respond on those a lot of the times.

2      

Hi, have you tried adding the fonts directly in to the .swiftpm, it should automatically create a Resources folder, after that you would get your font:

struct ContentView: View {

    @State private var font: Font?

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
                .font(font)

        }
        .task {
            getFont()
        }
    }

    func getFont() {
        let cfURL = Bundle.main.url(forResource: "Loving Stay", withExtension: "otf")! as CFURL

        CTFontManagerRegisterFontsForURL(cfURL, CTFontManagerScope.process, nil)

        let uiFont = UIFont(name: "Loving Stay", size:  30.0)

        font = Font(uiFont ?? UIFont())
    }
}

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.