ios UIFont fontWithName 返回 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10255141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIFont fontWithName returns nil
提问by 0xSina
In my info.plist, I added a key "Fonts provided by application" and added EdwardianScriptITCStd.otf
在我的 info.plist 中,我添加了一个键“应用程序提供的字体”并添加了 EdwardianScriptITCStd.otf
But, when I try to get that font, it returns nil:
但是,当我尝试获取该字体时,它返回 nil:
NSLog(@"%@", [UIFont fontWithName:@"EdwardianScriptITCStd"
size:16.0f]);
I printed the font families using [UIFont familyNames]
and it's not there either:
我使用打印字体系列[UIFont familyNames]
,它也不存在:
(
Thonburi,
"Snell Roundhand",
"Academy Engraved LET",
"Marker Felt",
"Geeza Pro",
"Arial Rounded MT Bold",
"Trebuchet MS",
Arial,
Marion,
"Gurmukhi MN",
"Malayalam Sangam MN",
"Bradley Hand",
"Kannada Sangam MN",
"Bodoni 72 Oldstyle",
Cochin,
"Sinhala Sangam MN",
"Hiragino Kaku Gothic ProN",
Papyrus,
Verdana,
"Zapf Dingbats",
Courier,
"Hoefler Text",
"Euphemia UCAS",
Helvetica,
"Hiragino Mincho ProN",
"Bodoni Ornaments",
"Apple Color Emoji",
Optima,
"Gujarati Sangam MN",
"Devanagari Sangam MN",
"Times New Roman",
Kailasa,
"Telugu Sangam MN",
"Heiti SC",
"Apple SD Gothic Neo",
Futura,
"Bodoni 72",
Baskerville,
"Chalkboard SE",
"Heiti TC",
Copperplate,
"Party LET",
"American Typewriter",
"Bangla Sangam MN",
Noteworthy,
Zapfino,
"Tamil Sangam MN",
"DB LCD Temp",
"Arial Hebrew",
Chalkduster,
Georgia,
"Helvetica Neue",
"Gill Sans",
Palatino,
"Courier New",
"Oriya Sangam MN",
Didot,
"Bodoni 72 Smallcaps"
)
What am I missing? Thanks!
我错过了什么?谢谢!
回答by inspector-g
My comments were starting to get lengthy, so I've decided to submit an answer instead :)
我的评论开始变得冗长,所以我决定提交一个答案:)
Custom fonts are only supported in iOS 3.2 and above, so make sure your Deployment Target build setting doesn't go back any farther than version 3.2.
Verify that the font is included in your build target (in the "Copy Bundle Resources" build phase)
Check the raw value of the key for "Fonts provided by application" to make sure it's correct. When viewing the *.plist in Xcode, right-click and choose "Show Raw Keys/Values", then verify the key is
UIAppFonts
.I believe iOS requires the PostScript name for a font when using
fontWithName: size:
, which you can find/verify by opening the font in Apple's Font Book and typingcommand+I
.At one point, there was an issue with using custom fonts and
UILabel
. I am not sure if this problem still exists. The solution was to subclassUILabel
and set the font therein.
自定义字体仅在 iOS 3.2 及更高版本中受支持,因此请确保您的部署目标构建设置不会回溯到版本 3.2 之前。
验证字体是否包含在您的构建目标中(在“复制捆绑资源”构建阶段)
检查“应用程序提供的字体”键的原始值以确保它是正确的。在 Xcode 中查看 *.plist 时,右键单击并选择“Show Raw Keys/Values”,然后验证密钥为
UIAppFonts
。我相信 iOS 在使用时需要字体的 PostScript 名称
fontWithName: size:
,您可以通过在 Apple 的 Font Book 中打开字体并输入command+I
.曾几何时,使用自定义字体和
UILabel
. 我不确定这个问题是否仍然存在。解决方案是子类化UILabel
并在其中设置字体。
If it still doesn't work, this Stack Overflow thread on custom fontshas quite a bit of info, you may have some luck there.
如果它仍然不起作用,这个关于自定义字体的 Stack Overflow 线程有很多信息,你可能在那里有一些运气。
EDIT
There's no guarantee that I have the same font file as you, but on my machine the PostScript name is "EdwardianScriptITC"
编辑
不能保证我和你有相同的字体文件,但在我的机器上,PostScript 名称是“EdwardianScriptITC”
回答by BeyazBaron
If your font name include '-' character, replace it with space character. For instance, Arial-Bold.ttf is used like [UIFont fontWithName:"Arial Bold" size:10];
如果您的字体名称包含“-”字符,请将其替换为空格字符。例如,Arial-Bold.ttf 的用法类似于 [UIFont fontWithName:"Arial Bold" size:10];
回答by user3812138
i was having the same issue. you can set font to a label from interface builder at storyboard. then get that font in debug mode from label like :
我遇到了同样的问题。您可以在情节提要的界面构建器中将字体设置为标签。然后在调试模式下从标签中获取该字体,例如:
let cfnt = txtUsernameField.font
then click the i button on the little debug window and the family name of font will be writed on the console window. and you can use it like that :
然后单击小调试窗口上的 i 按钮,字体的系列名称将被写入控制台窗口。你可以这样使用它:
txtUsernameField.font = UIFont(name: "MyriadPro-CondIt", size: 14)
回答by Sagi Mann
The font name to be used seems to be the "Family name" in the Font Book. I tried the PostScript name, but it does not always work. Family Name, as far as I can tell, has always worked up until now.
要使用的字体名称似乎是字体手册中的“家族名称”。我尝试了 PostScript 名称,但它并不总是有效。据我所知,姓氏一直有效到现在。
You can check this information, as mentioned in the above answer, inside Font Book, select the font and press cmd+I.
您可以检查此信息,如上述答案中所述,在字体簿中,选择字体并按 cmd+I。
Hope this helps...
希望这可以帮助...
回答by rmigneco
I had this problem although I swore I had custom fonts working just fine previously.
我有这个问题,虽然我发誓我以前自定义字体工作得很好。
Turns out I just needed to run "Clean" on my project in XCode project so it could re-establish the references.
原来我只需要在 XCode 项目中对我的项目运行“Clean”,这样它就可以重新建立引用。
回答by Mark Suman
The problem I ran into is that some of the fonts from the font family returned correctly while others returned nil
.
我遇到的问题是字体系列中的某些字体正确返回,而其他字体返回nil
.
For example:
例如:
MuseoSansRounded-500
validMuseoSansRounded-900
nil
MuseoSansRounded-500
有效的MuseoSansRounded-900
零
It turned out that Xcode was automatically including custom fonts that had been used in Interface Builder. I had used the 500 weight font in IB but not the 900 weight one.
结果是 Xcode 自动包含了在 Interface Builder 中使用过的自定义字体。我在 IB 中使用了 500 重量的字体,但没有使用 900 重量的字体。
These two steps fixed it for me:
这两个步骤为我修复了它:
In Build Settings, find and disable
Auto-Activate Custom Fonts
. There were two instances of this in my project.On app launch, manually register the custom fonts with the system. I used the snippet from this SO answer: https://stackoverflow.com/a/32600784/131378
在构建设置中,找到并禁用
Auto-Activate Custom Fonts
. 我的项目中有两个这样的实例。在应用程序启动时,向系统手动注册自定义字体。我使用了这个 SO 答案中的片段:https: //stackoverflow.com/a/32600784/131378
In the name of completeness, I could have left Auto-Activate Custom Fonts
turned on and then skipped registering those fonts in code. However, I didn't want to update the code during development if I ended up using a new font in IB that was previously being registered manually. Xcode complains if you register a font that was already auto-activated.
以完整性的名义,我本可以保持Auto-Activate Custom Fonts
打开状态,然后跳过在代码中注册这些字体。但是,我并不想,如果我结束了使用先前被手动注册IB新字体的开发过程中更新的代码。如果您注册了一种已经自动激活的字体,Xcode 会抱怨。