5

我正在尝试在我的项目中使用这种字体,但它不起作用。我在我的项目中添加了 .ttf 文件,并将其名称添加到 MyApp-Info.plist 中的键下:Fonts provided by application。然后我尝试了这个:

 [_titleLabel setFont:[UIFont fontWithName:@"chalkboard" size:_titleLabel.font.pointSize]];

我还检查了字体的名称是否真的是“黑板”,它是。显示的字体仍然是 Helvetica,只是更小了。问题可能出在哪里?提前致谢。

4

6 回答 6

32

You need to use font name, not filename. Font name is inside the ttf file.

You can find font names for iOS here: http://iosfonts.com/

This code will list all your font names in your app, put it somewhere in viewDidLoad on main controller, run app and then in console found the right name for the font.

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];

NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    fontNames = [[NSArray alloc] initWithArray:
                 [UIFont fontNamesForFamilyName:
                  [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
    }
}

Upd.

You can also right click on font file in Finder, get Info and copy full name. But it is not always 100%.

于 2012-02-24T17:51:38.540 回答
24

Flinks answer 是一个方便的工具,可以检查您的字体是否正确加载。

请执行下列操作:

[_titleLabel setFont:[UIFont fontWithName:@"chalkboard" size:_titleLabel.font.pointSize]];

确保你的字体包含在你的包中:

  • 点击您的项目
  • build phases
  • 在 下'Copy Bundle Resources',确保包含您的字体

为我工作:)

于 2012-03-26T11:00:49.117 回答
3

Xcode 4.3.1 自定义字体有问题。因为当我创建新项目并尝试在 Xcode 4.2.1 中添加自定义字体时没有问题。

我做了几次。有趣的...

于 2012-03-20T08:14:51.980 回答
1

根据我的经验,并非“所有”字体都兼容。大多数工作,但你会遇到一些字体,由于某种原因,无论你多么努力,代码都无法识别它们......只是提醒任何有问题的人,试试另一种字体!

于 2013-04-21T13:43:02.153 回答
1

  1. 在你的 XCode 项目中包含你的字体
  2. 确保它们包含在 File Inspector 选项卡中的 Target Membership 中
  3. 在 Project Name > Build Phase > Copy Bundle Resources 中包含你的字体
  4. 在您的应用程序列表中包含您的 iOS 自定义字体。添加一个名为“应用程序提供的字体”的新行,并将您的字体名称和扩展名作为参数。
  5. 找到字体的名称。
  6. 使用 UIFont 并指定字体的名称。

    UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 290, 300, 30)];
    headerLabel.text = @"欢迎";
    headingLabel.font = [UIFont fontWithName:@"Arvo" size:14.0];

来源:http ://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

于 2013-09-13T08:02:54.827 回答
0

并非您导入的所有字体都有效。只是尝试测试另一种字体。如果它有效,则 Xcode 无法识别它。请记住,它必须在您的项目 plist 文件中,并且必须是 plist 中的“.otf”或“.ttf”之类的扩展名。并将其添加到目标下的“复制捆绑资源”中

于 2014-03-18T07:50:00.983 回答