Reputation: 6885
I was trying to add custom font to my today extension but UIFont
always return nil
.
Steps:
TodayViewController
of Today Widget but it always return nil
:- (void)viewDidLoad {
[super viewDidLoad];
UIFont* ft = [UIFont fontWithName:@"octicons-local" size:20];
}
I used the same method in my main project, and I can get the custom font. How can I fix it?
Upvotes: 11
Views: 3225
Reputation: 5436
Maybe it's because you forgot to add key in your .plist
file.
Add the key Fonts provided by application
to a new row. Add items for each font you have added.
Upvotes: 21
Reputation: 1892
I met this problem also, and I DID targeting font to my target
and put fonts providing by application
in plist. But still gets nil when I tried to retrieve font.
Maybe because the font name you're using is wrong.
For Example (also the problem I met)
The Font which I dragged into my project name is trenolt.otf
But Use this name UIFont(name: "trenolt", size: 14)
gets nil
And According to Adding a Custom Font to Your App By Developer Documentation
I print out all the font familys and names
for family in UIFont.familyNames.sorted() {
let names = UIFont.fontNames(forFamilyName: family)
print("Family: \(family) Font names: \(names)")
}
Then I get in console
["TruenoLt", "TruenoRg"]
And now use the printed name
UIFont(name: "TruenoLt", size: 14)
Gets
<UICTFont: 0x7fbc39a168b0> font-family: "Trueno"; font-weight: normal; font-style: normal; font-size: 14.00pt
Upvotes: 2
Reputation: 72
I had the same issue while working on an app extension. Some answers here helped me to solve it, but I wanted to make it clear and maybe easier for other devs facing the same problem as I did:
While the Fonts were working in the App, they did not in the extension, which caused a crash because
UIFont(name: name.rawValue, size: size)
returned nil
.
The fonts were in the Info.plist
file of the app itself, but not in the Info.plist
of the app extension. So I had to manually add a row to the extension's Info.plist
with the key Fonts provided by application
and enter all fonts of the app's Info.plist
file until they matched. Then it worked for me.
Upvotes: 0
Reputation: 195
I had same problem and in my case adding full file name including file extension under the plist key Fonts provided by application
worked for me.
Upvotes: 1
Reputation: 1181
I had the same issue and solved by installing the fonts. So here are the steps:
Right click on your .otf file from Project Navigator-> Click on show in finder -> double click the font from the finder window and you will see "install" button in the bottom right corner, click on install and then check in your app.
Hope this works.
Upvotes: 0