Custom font iOS in Today Widget always return nil
Asked Answered
P

5

11

I was trying to add custom font to my today extension but UIFont always return nil.

Steps:

  1. I added the font file to Today Widget target:

enter image description here

  1. I check if the font file was inside the Today Widget bundle:

enter image description here

  1. I init the font instance in the TodayViewController of Today Widget but it always return nil:
- (void)viewDidLoad {
    [super viewDidLoad];
    UIFont* ft = [UIFont fontWithName:@"octicons-local" size:20];
}

enter image description here

I used the same method in my main project, and I can get the custom font. How can I fix it?

Philosophy answered 22/2, 2016 at 12:33 Comment(0)
C
21

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.

Carbamate answered 22/2, 2016 at 12:36 Comment(2)
I think I don't have to add this key in new version of project,but I am wrong.Thank you .Philosophy
Great! you solved your problem:) and you are welcome@PhilosophyCarbamate
S
1

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. enter image description here

Skeie answered 9/3, 2017 at 19:33 Comment(0)
E
1

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
Excessive answered 22/5, 2018 at 7:0 Comment(0)
W
0

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.

Workhorse answered 22/2, 2016 at 12:48 Comment(0)
E
0

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.

Elutriate answered 15/5, 2018 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.