I had a custom font created and I'm using it in my application built in swiftui.
Since they liked the font, I would like to use it not only in the application but also on all the iPhones and iPads that have my application installed. I searched the internet a bit and came up with this:
- The font must be in a folder inside the project;
- I have to register the font in "Fonts provide by application";
- In "Capabilities" add "Fonts" and activate the two items: Install Fonts and Use Installed Fonts;
- If my application is uninstalled, the font will automatically no longer be available to the operating system;
Most importantly I understood that I have to use this class CTFontManagerRegisterGraphicsFont. But I don't understand how to use it.
Many tutorials show how to use a font in your application but none ever share the imported fonts with the device.
How can I go on? I also searched this site but couldn't find anything. Please don't report it as a DUPLICATE POST
CodePudding user response:
I found the solution to my problem. I hope it is useful to others.
var fontUrl = Bundle.main.url(forResource: "MyCustomFont", withExtension: "ttf")
//I apply the registration
registerFont(fontUrl: fontUrl!)
.....
private func registerFont(fontUrl : URL){
CTFontManagerRegisterFontURLs([fontUrl] as CFArray, .persistent, true) { (errors, done) -> Bool in
if(done) {
print("Done")
}
print(errors as Array)
return true
}
}
When you run the application you will get the pop up to install the font.
At this point: Settings > General > Font You will find the font