0

I have setup a Swift Package Manager with custom fonts for my iOS application.

I have three different types, and none of them are read and displayed in my SwiftUI app.

What is the proper way to setup and use a Swift Package Manager with custom fonts in it?

In my SPM, I have added my three different fonts, in a Font folder that I process in Package.swift. The font files are in the .otf format:

Font folders

let package = Package(
  name: "MyAwesomePackage",
  platforms: [
    .iOS(.v14)
  ],
  products: [
    .library(
      name: "MyAwesomePackage",
      targets: ["MyAwesomePackage"]),
  ],
  targets: [
    .target(
      name: "MyAwesomePackage",
      resources: [
        .process("Fonts") // Processing my custom fonts
      ])
  ]
)

This is the code I am using in my Swift Package Manager to use my custom font in my SwiftUI app:

public extension Text {
  // This is the modifier used in the SwiftUI app.
  func customFont(_ type: CustomFontType) -> some View {
    
    return self
      .font(.custom(type.customFont,
                    size: type.size,
                    relativeTo: type.nativeTextStyle))
  }
}

public enum CustomFontType {
  
  case body
  case header
  case link
  
  var customFont: String {
    switch self {
    case .header:
      return "Gotham-Bold"
    case .body:
      return "Gotham-Book"
    case .link:
      return "Gotham-Medium"
    }
  }
  
  var size: CGFloat {
    switch self {
    case .header:
      return 32
    case .body:
      return 14
    case .link:
      return 12
    }
  }
  
  var nativeTextStyle: Font.TextStyle {
    switch self {
    case .header:
      return .largeTitle
    case .body:
      return .body
    case .link:
      return .footnote
    }
  }
}

And this is how I call my custom font in my SwiftUI app. The thing is that I do not have any of my font being populated when called with the .customFont(_:) modifier, and the font provided by the app is not even the Apple SFFont ones.

import MyAwesomePackage
import SwiftUI

struct ContentView: View {

  var body: some View {
    Text("Custom Font")
      .customFont(.header)

    Text("Apple Font")
      .font(.largeTitle)
      .fontWeight(.bold)
  }
}
Roland Lariotte
  • 2,606
  • 1
  • 16
  • 40
  • You need to register the font. See my answer here that shows how I accomplished it. https://stackoverflow.com/questions/62681206/xcode-12b1-swift-packages-custom-fonts/62691001#62691001 – Andrew Aug 09 '21 at 10:17
  • This is not the expected solution as you call the bundle of the module inside your AppDelegate, in your iOS app, which does not provide a smart use of a SPM. I would like to only use the modifier `customFont(_:)` in my iOS app, and do all the needed job inside the Swift Package Manager. – Roland Lariotte Aug 09 '21 at 10:51
  • 1
    You still need to register the font with the app otherwise you won’t be able to use it. Register the font when you call `customFont`. However, be aware that you don’t want to register it more than once otherwise you may get errors. Also you should be doing those more intensive tasks at app launch rather than else where. – Andrew Aug 09 '21 at 12:46

0 Answers0