custom font not working programmatically in swift
Solution 1
Let's say you want to add this font: SourceSansPro-Regular.otf
Four steps:
- Add the font file
SourceSansPro-Regular.otf
to your project, make sure you select your target in the "Add to targets".
Go to the target's Build Phases and make sure it is under Copy Bundle Resources. If not, add it.
2. Go to the target's Info. Add a new entry Font provided by application then add a new item with this value SourceSansPro-Regular.otf
.
- From your finder, double click on the file
SourceSansPro-Regular.otf
, it might ask you to install the font to your Font Book.
- Open the OS X Font Book application, navigate to your font then press Command+i. Note the PostScript name and use that name in your Swift code. In this case, it's
SourceSansPro-Regular
.
So in your code:
labeladd.font = UIFont(name:"SourceSansPro-Regular", size:15)
Solution 2
I've found (XCode 12) that unless your font happens to be already used in your Main.storyboard, that even if you have done all the correct steps of registering the font file in your app, copied to your bundle resources, used the PostScript name, yadda, yadda, that it still didnt show up and trying to use it in UIFont(name: size:) would still return nil. :-(
I worked around this current bug (?) by manually registering all my custom fonts at startup in AppDelegate application(_ application: didFinishLaunchingWithOptions:)
let fonts = Bundle.main.urls(forResourcesWithExtension: "ttf", subdirectory: nil)
fonts?.forEach({ url in
CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
})
(adapted from Xcode: Using custom fonts inside Dynamic framework)
Solution 3
Sometimes the name of the font is not the name of your file. I was trying to use the NexaBook.otf font with the name "NexaBook" and at the end the problem was that the right name of the font is "Nexa-Book". To check you are using the right name write this code in the viewDidLoad and check the name of the font:
for family: String in UIFont.familyNames
{
print(family)
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}
In my case I got:
...
Nexa
== Nexa-Book
Avenir Next
== AvenirNext-Medium
== AvenirNext-DemiBoldItalic
== AvenirNext-DemiBold
...
I get this idea from Common Mistakes With Adding Custom Fonts to Your iOS App where you can find a great description of the problems working with custom fonts.
Solution 4
(1)First select the "Font" folder then make sure it's selected.
Solution 5
If you are getting the font in storyboard but not in font family and are not able to set it progammatically, then the reason is that the Xcode might not have compiled the font. To solve it:
- Set the required font(say: Roboto Bold) as the font of any UI element on storyboard E.g UILabel.
- Run the project
- Now check the font family names. The required font(say: Roboto Bold) name would have appeared in it.
- Now you can set the required font(say: Roboto Bold) progammatically :)
bLacK hoLE
I’ve been developing apps for iPhones and iPads.I’m a Entrepreneurial App Developer & Consultant. My client work is exciting and diverse. As well as being a very experienced coder I’ve also attained a wealth of knowledge of consumer and enterprise app distribution and am regularly commissioned to consult and help clients make the most of the incredible Apple technologies that have become such a core part of our culture over the last 3 years
Updated on July 14, 2022Comments
-
bLacK hoLE almost 2 years
I've followed the step to to add custom fonts in xcode at swift day-by-day and custom fonts but I'm not able to set that font in app label programmatically.
var labeladd = UILabel(frame: CGRectMake(40, 50, 70, 22)) // label.center = CGPointMake(160, 284) /// labeladd.font=UIFont.boldSystemFontOfSize(15) labeladd.font = UIFont(name:"Source Sans Pro",size:15) labeladd.textColor=UIColor.blackColor() labeladd.textAlignment = NSTextAlignment.Center labeladd.text = "this is custom fonts" myview.addSubview(labeladd)
-
Dinesh Raja almost 9 yearsThat really helped. I missed to add extension in the end of font name in .plist file.
-
Kex over 8 yearsWow the Font Book part fixed it for me. Didn't realise the PostScript name would be different. Cheers
-
Admin almost 8 yearsI also didn't realize that I needed to use the PostScript name. After using that it took right off. Thanks!
-
abhinavroy23 over 6 yearsI can't find the font in my font book application :( I am trying to add Philosopher-Regular.ttf
-
Van Du Tran over 6 yearsFrom your finder, double click on Philosopher-Regular.ttf, it might ask you to install the font to your Font Book.
-
Coder ACJHP about 5 yearsGood way for debuging font names, I was missing "W03" in CirceRoundedW03-Regular.
-
Ahsan Ebrahim almost 5 yearsExcellent detailed yet simple explanation, I used to be scared of using fonts programmatically but not anymore. Thanks a lot.
-
Bavafaali over 4 yearsThanks. That postscript name saved my life!
-
Jonas Deichelmann about 4 yearsStep 1 solved my Problem. I tried everything else. Do you know why?
-
leshow almost 4 yearsStill not clear why this is needed in 2020, but it does work in loading the fonts correctly.
-
Anees over 3 yearsEvery steps are right , but I can't find the font family in the Logged list. not found in storyboard, can't set programatically
-
Anees over 3 yearscopied to bundle->checked the target->checked info.plist->checked copy bundle resources-> still the Font family is not able find in the Log. Nobody Is commenting on what if did not find the Font in Logged list.
-
Shivani Bajaj over 3 years@Anees were you not able to get the font in font family, as per the given steps as well?
-
Matthew So about 3 yearsIt nailed it, especially for point#3! Thanks!
-
Ameya Vichare about 3 yearsWow, what a weird bug
-
Codetard almost 3 years.otf and .ttc was my case and it helped.