Skip to content

Instantly share code, notes, and snippets.

@orleonedev
Created April 21, 2022 18:19
Show Gist options
  • Save orleonedev/3f59151674a971dfcb8b1b7b1e81002c to your computer and use it in GitHub Desktop.
Save orleonedev/3f59151674a971dfcb8b1b7b1e81002c to your computer and use it in GitHub Desktop.
Custom fonts for Swiftpm
import Foundation
import SwiftUI
public struct MyFonts {
public static func registerFonts() {
registerFont(bundle: Bundle.main , fontName: "YOUR-FONT-HERE", fontExtension: ".ttf") //change according to your ext.
}
fileprivate static func registerFont(bundle: Bundle, fontName: String, fontExtension: String) {
guard let fontURL = bundle.url(forResource: fontName, withExtension: fontExtension),
let fontDataProvider = CGDataProvider(url: fontURL as CFURL),
let font = CGFont(fontDataProvider) else {
fatalError("Couldn't create font from data")
}
var error: Unmanaged<CFError>?
CTFontManagerRegisterGraphicsFont(font, &error)
}
}
/*
Remember then to add inside your main file before var body
init(){
MyFonts.registerFonts()
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment