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)
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)")
}
}
Swift 4 set custom font programmatically
You can try
lbl.font = UIFont(name:"FontAwesome",size:15)
the name should be the font name as it is when you install it . not as the file name in your case was Noto Kufi Arabic instead of NotoKufiArabicRegular
click the font and open it with Font Book , then install it after that specify exactly the name shown in the parameter in the line above
How to use a custom font in an entire IOS app the right way
Based on this site and making some adjustments
First: You must add your fonts in your project and add them in the info.plist
file:
https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
File: UIFont.swift
import Foundation
import UIKit
struct AppFontName {
static let regular = "Montserrat-Regular"
static let bold = "Montserrat-Bold"
static let lightAlt = "Montserrat-Light"
}
//customise font
extension UIFontDescriptor.AttributeName {
static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
}
extension UIFont {
@objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.regular, size: size)!
}
@objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.bold, size: size)!
}
@objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.lightAlt, size: size)!
}
@objc convenience init(myCoder aDecoder: NSCoder) {
guard
let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
self.init(myCoder: aDecoder)
return
}
var fontName = ""
switch fontAttribute {
case "CTFontRegularUsage":
fontName = AppFontName.regular
case "CTFontEmphasizedUsage", "CTFontBoldUsage":
fontName = AppFontName.bold
case "CTFontObliqueUsage":
fontName = AppFontName.lightAlt
default:
fontName = AppFontName.regular
}
self.init(name: fontName, size: fontDescriptor.pointSize)!
}
class func overrideInitialize() {
guard self == UIFont.self else { return }
if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
}
if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
}
if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
}
if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))), // Trick to get over the lack of UIFont.init(coder:))
let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
}
}
}
AppDelegate.swift
override init() {
super.init()
UIFont.overrideInitialize()
}
Usage:
label.font = UIFont.boldSystemFont(ofSize: 16)
label.fontUIFont.systemFont(ofSize: 12)
Is is possible to use an imported custom font for a button in swift UI ios app?
Make sure you add the fonts correctly to your project..
check this link
Use the correct font name
button.titleLabel?.font = UIFont(name: "Lovelo Black", size: size)
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
Custom Font in swift
This is pretty easy to do, actually. Firstly, you need to make sure that the font file is in your app file hierarchy:
you can see mine here "small_pixel.ttf"
Secondly you need to make sure that the info.plist
file has the EXACT file name as your custom font:
you can see that here.
Thirdly, you need to make sure that the font is included in bundle resources
you can navigate to it by going:
to add it to bundle resources
simply drag and drop the file into the hierarchy.
Finally you can see all of your fonts by doing...
for family in UIFont.familyNames {
print("Family: \(family)")
for name in UIFont.fontNames(forFamilyName: family) {
print(" - \(name)")
}
}
...in some function that gets called when you launch your app. Simply scroll down through the Debug area until you find your font.
(ps. this is from my first really trashy game. If you want to check it out, search "Crappy Duck" on the AppStore)
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.
Related Topics
Is Self Captured Within a Nested Function
Duplicating a Particle Emitter Effect in Sprite Kit
Uipopovercontroller, Xcode 6, iOS 8 Using Swift
Reading from the Clipboard with Swift 3 on MACos
Difference Between Dispatchqueue Types in Swift
iOS 11 Black Bar Appears on Navigation Bar When Pushing View Controller
Uicollectionviewcell Register Class Fails, But Register Nib Works
Ios-Charts Library: X-Axis Labels Without Backing Data Not Showing
What Is the Swift 3 Equivalent of Nsurl.Urlbyappendingpathcomponent()
Swift Combine - Wait for All Publishers
Add Placeholder to Uitextfield, How to Set the Placeholder Text Programmatically in Swift
How to Implement Hash(Into:) from Hashvalue in Swift
Building User Database Model in Firebase
Xcode 11 Doesn't Recognize Core Data Entity
Uiimage Created from Mtkview Results in Color/Opacity Differences
Swiftui - How to Set the Title of a Navigationview to Large Title (Or Small)
Delegate Using Container View in Swift
How to Get the Yaw, Pitch, Roll of an Aranchor in Absolute Terms