Swift Custom Fonts Xcode
These are the steps to add a custom font to you application:
- Add "gameOver.ttf" font in your application ( Make sure that it's included in the target)
- Modify the application-info.plist file.
- Add the key "Fonts provided by application" in a new row
- and add "gameOver.ttf" as new item in the Array "Fonts provided by application".
Now the font will be available in Interface Builder.
To use the custom font in code we need to refer to it by name, but the name often isn’t the same as the font’s filename
There are 2 ways to find the name:
- Install the font on your Mac. Open Font Book, open the font and see
what name is listed.- Programmatically list the available fonts in your app
for the second approach add this line is your app delegate’s didFinishLaunchingWithOptions
print(UIFont.familyNames)
To list the fonts included in each font family, in Swift 5:
func printFonts() {
for familyName in UIFont.familyNames {
print("\n-- \(familyName) \n")
for fontName in UIFont.fontNames(forFamilyName: familyName) {
print(fontName)
}
}
}
after finding the name of your custom font you may use it like this:
SKLabelNode(fontNamed: "gameOver") // put here the correct font name
or in a simple label :
cell.textLabel?.font = UIFont(name: "gameOver", size: 16) // put here the correct font name
Useful resource
Adding custom font to Xcode 13+
- drag and drop your font-file into the project (make sure you hit copy if needed and check the target box)
- choose Project -> Targets -> Info -> Custom iOS Target Properties and klick the small + icon under the "key" property. Start typing "Fonts provided by application"
- enter the full font-filename inside this property-list
now you can use the font by its name
(Bonus: if you have trouble to find the font name, you can print all available fonts with [see detail explanation for this at code with chris ]:
for family: String in UIFont.familyNames
{
print(family)
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}
How to change font in Xcode Swift Playgrounds(.swiftpm) project?
I found the solution by myself with the help of a friend, I had to create a public struct MyFont with a fileprivate static func, and then add an init inside the main file, before var body.
This is the code:
MyFont
import SwiftUI
public struct MyFont {
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)
}
}
MyApp
@main
struct MyApp: App {
//add the init before var body
init() {
MyFont.registerFonts()
}
var body: some Scene {
//
}
}
Custom Fonts not showing on Xcode 11 (macOS Catalina)
Unfortunately, the only solution for me was a fresh install of mac os catalina. Now everything works correctly in xCode interface builder.
iOS Custom font without adding to system
A storyboard does not magically look in your project for fonts. So opening a storyboard will not display a font unless that font is available on your machine as a whole.
The usual approach is to set the font in code, not in the storyboard.
Use custom font in swift
Fist make sure that your font is .ttf or .otf format
- Import your font into the project
- Add new key "Fonts provided by application" on application's info.plist file and add your font names
Now you can use custom fonts with interface builder or programatically
yourLabel.font = UIFont.init(name: YourFont, size: size)
Custom font not loading in SwiftUI project after moving code to another project?
Although it looks like you have already done this, just go through the following checklist once:
- Fonts (.ttf files) are present inside the project
- Entry in info.plist file
- Entry in "Copy Bundle Resources" under "Build Settings" of your target
If this is missing, add by clicking on the plus (+) icon in this section - Make sure you are using the font correctly (check spelling errors?)
var FontRegular : Font = Font.custom("Poppins-Regular", size: 16)
var FontBold : Font = Font.custom("Poppins-Bold", size: 16)
...
Text("Sample Text")
.font(FontRegular)
Related Topics
How to Check If iOS App Is in Background
Left-Align Image and Center Text on Uibutton
How to Add a Uitoolbar Programmatically to an iOS App
How to Disable Calayer Implicit Animations
How to Know If Nsassert Is Disabled in Release Builds
New Foursquare Venue Detail Map
How to Determine Device Type from Swift? (Os X or iOS)
How to Capture Picture with Avcapturesession in Swift
Move a View When Scrolling in Uitableview
Expanding and Collapsing Table View Cells in iOS
Show Search Bar in Navigation Bar Without Scrolling on iOS 11
Setting Maximum Number of Characters of 'Uitextview ' and 'Uitextfield '
Perform Push Segue After an Unwind Segue
Loading a Resource (E.G. Storyboard) in a Swift Framework
iOS Changing Uiscrollview Scrollbar Color to Different Colors
What Do JSONserialization Options Do and How Do They Change JSONresult
How to Stop Uitableview from Clipping Uitableviewcell Contents in iOS 7