Custom font not showing on device, but does on sim

2019-06-15 20:26发布

问题:

I've been playing around with custom fonts in iOS and have got most of it figured out apart from the custom font won't show on any of my devices.

  1. I have added the font to the root of the project file.
  2. Added it to the info.plist file under the array or "Fonts provided by application"
  3. Checked that the case sensitivity of both the original file and what I added to the info.plist file are the same.
  4. Used [playMenuBtn.titleLabel setFont:[UIFont fontWithName:@"fontname" size:40]]; to make the font work.

When I run my app in the Simulator it all appears fine and works. However as soon as I run it on a physical device, it displays the default font with the default size.

If I use:
UIFont *font = [UIFont fontWithName:@"VINCASTENCIL" size:20]; NSLog(@"Font:%@", font);

Then the simulator returns: 2012-04-07 01:10:41.544 AppName[47412:f803] Font:<UICFFont: 0x683e040> font-family: "VINCA STENCIL"; font-weight: normal; font-style: normal; font-size: 20px

Where as the device returns: 2012-04-07 00:42:56.358 AppName[5079:707] Font:(null)

I have tried everything, including trying all the names that FontBook tells me the font could be named. I know it's free and there isn't a licence on it.

Any help would be greatly appreciated as it's driving me nuts and I really don't want to use images for al of these as localisation would then be a pain!

回答1:

Right, I've just managed to get this working. Everything I was doing was correct but for some reason it wasn't adding my font file to 'Targets' -> 'Build Phases' -> 'Copy Bundle Resources'. By manually adding it to this list, the font then shows on the device.

This is the second time this project has not added a file here automatically (it's done it previously with a class I added, and it didn't) so I think it's something wrong with my project, or a bug in Xcode.

Thanks to everyone for your suggestions and help. :-D



回答2:

Yes, follow all the directions mentioned above and you should be fine. Something else to look out for: case sensitivity. In your *.plist file and Fonts provided by application. The simulator might ignore capitalization, but the device will not. For example, 'Facebook' is not the same as 'FaceBook'. (This led to the font working in the simulator, but not the device!)