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