Can't use custom font in iOS app

[亡魂溺海] 提交于 2019-11-29 23:59:40

问题


I'm trying to use custom font for my iOS app. Here is what I do

I added my custom font (m.tff) to Xcode Supporting files directory. Also I create entry called Fonts provided by application with value name of the font (m.tff) in myApp.plist

In my view I have a label

UILabel *label = [[UILabel alloc]initWithFrame:CGRect(10, 20, 230, 30)];
label.font = [UIFont fontWithName:@"My custom font" size:15];

etc...

The problem is that when I launch the app the text in label is with default font instead of using the specific one.

What I miss here ?

EDITED

Well it seem works now, but the font applies only for characters like , ? but not for letters. I'm using cyrillic btw.


回答1:


Use this to get all the fonts available

  // List all fonts on iPhone
  NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
  for(NSString *familyName in familyNames)
  {
    NSLog(@"family: %@", familyName);

    NSArray *fontNames = [[NSArray alloc] initWithArray:[UIFont fontNamesForFamilyName:familyName]];
    for(NSString *fontName in fontNames)
    {
      NSLog(@"    font: %@", fontName);
    }
  }

Look for the font you embedded and see if it is the name is the same.




回答2:


Font extension should be ttf instead of tiff

and then make an entry in plist

Fonts provided by application Font.ttf




回答3:


Check to see if your [UIFont fontWithName:@"My font" size:15] line returns a UIFont object that is not null.




回答4:


In your Info.plist file, add the key: Fonts provided by application with an array object, and set the items of the array to strings as follows:

MyFont.ttf

Where MyFont is the EXACT name as listed for the font in your Mac's app, "Font Book." Use Spotlight to launch font book and then find your font file in there to double check the name.

Then when you set the font to your label:

myLabel.font = [UIFont fontWithName: @"MyFont" size: someFloatValue];



回答5:


m.tiff doesn't sound like a supported font file, i.e. TrueType or OpenType.




回答6:


  1. Make sure your fonts are in info.plist (Fonts provided by application)
  2. Select yor target, in the build phases tab, Copy Bundle Resources, add your fonts.


来源:https://stackoverflow.com/questions/9053597/cant-use-custom-font-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!