I have added a custom font to my project. It is included in the target, and it is added in the plist. When I try to use it programmatically it doesn\'t work, and it doesn\'t
I know this is old but I just ran into the same issue. The font would show up only if I selected that font in the interface builder. Turns out when I searched for UIAppFonts
, it didn't list the main Info.plist (we were already using custom fonts) for some reason. So I ended up adding the fonts to the localized (?) plist file which resulted in this issue. What I had to do was to go to the Project Settings > Target > Info tab and add the font file names there.
Ok, topic is old but I would like to put in my two cents here.
First off all, read this article http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
In brief:
When I did everything like described in this article, my fonts works in interface builder only. I also have problem with step 5 - my fonts did not show up in console output.
Ok, so now is time for tips from me:
TIP #1
use otf font format, if you have different, convert it (or ask your designer ;) ) you can use this site for that: https://onlinefontconverter.com/
TIP #2
In article above you can find:
Open it and add a new row called “Fonts provided by application” which will be an array that you need to add all the filenames of the fonts you want to use.
Instead of "Fonts provided by application" enter "UIAppFonts" Remember, if you have more than one plist file, set this for all.
Matt did a great job of isolating the problem... I was not loading the font correctly.
In my case the problem was in the Info.plist.
I was listing the font in the Info.plist using its name. This is incorrect. You are supposed to list the font in the Info.plist using its file name.
Issue is You are not giving ProperName Of Fonts in Code, for Getting Name of Fonts please run this code.
for family: String in UIFont.familyNames
{
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}
it will prints all fonts, search your font and give proper name.
let attributedText = NSMutableAttributedString(string: "We
Apologize! \n This feature is only available for \n Go 4.0
Subscriptions", attributes: [NSAttributedStringKey.font :
UIFont(name: "SpecialElite-Regular", size: 22)!])
When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts
This proves that you have not in fact included it properly in the target and the Info.plist.
The reason it seems to work in IB is that this font is also present on your computer. But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.
Your font is Special Elite. As you can see, I have it visible here in my running app:
Here's the code that I used:
let lab = UILabel()
lab.text = "This is a test"
lab.font = UIFont(name:"SpecialElite-Regular", size:18)
lab.sizeToFit()
lab.frame.origin = CGPointMake(100,100)
self.view.addSubview(lab)
So you see, it is possible to refer to this font in code — if it is loaded properly. You are evidently not loading it properly. It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist.
(Of course there's always a possibility that you're calling [UIFont fontWithName:size:]
with a bad value for the name or for the size.)
As mentioned by others, following article is very useful to diagnose the problem. http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
Also while adding custom fonts to Xcode project, add individual font and not the directory in which all fonts are stored. If you add that directory, fonts will be visible in Interface Builder but not in the running app.