IOS设备上未反映自定义字体

4

我按照这个教程中的步骤,将自定义字体显示在了我的Storyboard上,但是当我在模拟器或设备上运行我的应用时,字体并没有生效。有人能帮忙吗? 我所做的如下:

1- downloaded a .ttf file and copied it to my project
2- added "Fonts Provided By Application" in info.plist and added a row having the name of my custom font.
3- opened storyboard and selected the UILabel i want to have a custom font for, and selected the font as custom then selected my font. the font was shown on the storyboard correctly.

我有什么遗漏吗?


4
确保字体文件已包含在您的目标中:点击它,然后进入“文件检查器”(窗口右侧面板上的第一个选项卡),并检查“目标成员资格”部分中是否有一个勾号。 - jcaron
还要检查一下,在 Info.plist 文件中列出的是文件名,而不是字体名称。 - jcaron
1
如果你执行 NSLog(@"%@", [UIFont familyNames]); 会发生什么?你的字体族名称是否会在列表中显示? - Andrey Chernukha
复选框未被选中,所以我选择了它。这可能是因为我正在使用多种语言。当打印NSLog时,我看不到字体名称。 - Rashad.Z
选中复选框后,进行“Clean”操作,然后再次运行应用程序,查看是否现在打印出了 familyName(而不是字体名称!)。 - Andrey Chernukha
@rashad.z:请问您想使用哪种字体?是在选择自定义字体时“Family Name”下可用的那一种吗?还是有其他不同的字体? - neha_sinha19
3个回答

5
感谢 @Andrey Chernuka 和 @jcaron。我通过将目标成员设置为我的项目,然后退出项目(清理出了一些问题),重新打开项目,一切都解决了。当我打印 print (UIFont.familyNames()) 时,字体名称出现了。

1
你只需要按照以下步骤操作。
步骤1 - 下载.ttf文件并将其复制到我的项目中 步骤2 - 在info.plist中添加“应用程序提供的字体”,并添加一行包含我自定义字体的名称。

enter image description here

第三步 - 显示已添加的字体是否存在于项目中。将其复制并粘贴到 application:didFinishLaunchingWithOptions 中。
NSLog(@"Available fonts: %@", [UIFont familyNames]);
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]);
        }

    }

第四步 - 找到你的字体名称并在story board中进行同名检查。

它绝对有效。


1

你是否已经将.tff文件夹添加到支持文件中?然后像这样编辑info.Plist

应用程序提供的字体作为数组

第一项为黑体SC.tff

现在你可以设置 label.font = [UIFont fontWithName:@"Heiti SC" size:15];

然后你还需要将ttf文件添加到目标构建阶段的复制包资源中。


然后您还需要将ttf文件添加到目标构建阶段的复制包资源中。--> 这对我很有帮助!! - Mr. Bean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接