Created
April 21, 2022 18:19
-
-
Save orleonedev/3f59151674a971dfcb8b1b7b1e81002c to your computer and use it in GitHub Desktop.
Custom fonts for Swiftpm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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