简体   繁体   English

如何在我的xcode项目中添加和使用自定义字体

[英]How to add and use a custom font in my xcode project

I am trying to add and use a font within my app. 我想在我的应用程序中添加和使用字体。 I've followed this post for reference but I can't get it to work. 我已经关注这篇文章以供参考,但我无法让它发挥作用。

I've added the font Frutiger95UltraBlack.ttf as a supporting file and then added it to my {app-name}-Info.plist. 我添加了字体Frutiger95UltraBlack.ttf作为支持文件,然后将其添加到我的{app-name} -Info.plist中。

Then tried to use it in my app delegate to change my UINavegationBar but nothing happens. 然后尝试在我的app委托中使用它来更改我的UINavegationBar但没有任何反应。

What am i missing?? 我错过了什么?

 [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                          [UIColor whiteColor], UITextAttributeTextColor,
                                                          [UIFont fontWithName:@"Frutiger95UltraBlack" size:16.0], UITextAttributeFont,nil]];

(从我上面的评论:) [UIFont fontWithName:...]需要字体的PostScript名称作为参数,可能与文件名不同。

make sure that your font is in your Copy Bundle Resources. 确保您的字体在Copy Bundle Resources中。

if we not adds font to target then sometimes it happens that your font not automatically adds in Copy Bundle Resources. 如果我们不向目标添加字体,那么有时会发生您的字体不会自动添加到Copy Bundle Resources中。

also make sure that your font name is same that you have used. 还要确保您的字体名称与您使用的字体名称相同。 you can make this sure by opening font in fontBook. 你可以通过在fontBook中打开字体来确保这一点。

if you have different fonts like Regular/Bold/italic,then you have to attach this name to your font name like this- Frutiger95UltraBlack-Bold 如果您有不同的字体,如常规/粗体/斜体,那么您必须将此名称附加到您的字体名称,如下所示 - Frutiger95UltraBlack-Bold

install your font on mac system.iterate your all fonts programatically , write NSLog for fontName, by this way you can make sure the name of font ie PostScriptName. 在mac system上安装你的字体。编程所有字体,编写NSLog for fontName,通过这种方式你可以确保字体的名称,即PostScriptName。

Just an FYI, UITextAttributeTextColor and UITextAttributeFont were deprecated in iOS 7.0. 只是一个FYI, UITextAttributeTextColorUITextAttributeFont在iOS 7.0中已弃用。

Instead replace UITextAttributeTextColor with NSForegroundColorAttributeName and UITextAttributeFont with NSFontAttributeName 相反更换UITextAttributeTextColorNSForegroundColorAttributeNameUITextAttributeFontNSFontAttributeName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM