Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom font iOS in Today Widget always return nil

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?

like image 939
wj2061 Avatar asked Feb 22 '16 12:02

wj2061


2 Answers

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.

like image 62
Ronak Chaniyara Avatar answered Oct 12 '22 11:10

Ronak Chaniyara


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
like image 45
stan liu Avatar answered Oct 12 '22 09:10

stan liu