swiftxcodeswiftuiswift-playgroundipados

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


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.

Mac File Structure in Xcode Playgrounds


Solution

  • I figured it out!

    It seemed to be a problem with where I was keeping my fonts. You need to add the fonts to the Sidebar with iPadOS or macOS Playgrounds, not in Xcode. I was adding the fonts to a folder I made by myself in Xcode, which doesn't seem to be the same as the automatic Resources folder created on iPadOS and macOS Playgrounds created automatically, not manually.