简体   繁体   English

具有自定义字体的UILabel显示*错误*自定义字体

[英]UILabel with custom font displays *wrong* custom font

I'm using this method to embed custom fonts in my iPhone app. 我正在使用这种方法在iPhone应用程序中嵌入自定义字体。

The embedding works: When I run the following code, the fonts are listed. 嵌入的工作原理:当我运行以下代码时,将列出字体。 (Currently, I'm embedding all family members of Myriad Pro in OTF format) (当前,我以OTF格式嵌入Myriad Pro的所有家庭成员)

for( NSString *familyName in [UIFont familyNames] ){
    for( NSString *fntName in [UIFont fontNamesForFamilyName:familyName] ){
        NSLog(@"%@", fntName);
}}

When I try to set the font of a label to MyriadPro or MyriadPro-Bold, this works just as expected. 当我尝试将标签的字体设置为MyriadPro或MyriadPro-Bold时,这可以按预期工作。 However, when I set the font to MyriadPro-BoldCond, the label is still set in MyriadPro-Bold instead of the condensed version. 但是,当我将字体设置为MyriadPro-BoldCond时,标签仍设置为MyriadPro-Bold而不是压缩版本。 (The font names are correct, I checked.) My Code: (我检查了字体名称是否正确。)我的代码:

[recommendationLabel setFont:[UIFont fontWithName:@"MyriadPro-BoldCond" size:140]];
recommendationLabel.adjustsFontSizeToFitWidth = YES;

What's the deal? 这是怎么回事?

Edit: In addition to Ingve's solution below, I have found out that – at least for Myriad Pro – the correct font variant gets displayed as soon as I remove unused fonts from the plist. 编辑:除了下面的Ingve解决方案外,我发现–至少对于Myriad Pro –一旦从plist中删除未使用的字体,就会显示正确的字体变体。 In this case I needed to remove MyriadPro-SemiBold. 在这种情况下,我需要删除MyriadPro-SemiBold。 Mindboggling. 令人难以置信。

UIFont is limited to two variations per font family. UIFont每个字体系列仅限两个变体。 However, you can work around this by opening the additional n-2 fonts in an editor and changing the family name. 但是,您可以通过在编辑器中打开其他n-2字体并更改系列名称来解决此问题。

Answered my own question about this here: UIFont fontWithName: limited to loading 2 variations per family 在这里回答了有关此问题的我自己的问题: UIFont fontWithName:每个家庭只能加载2个变体

Maybe it is a limitation of UIFont? 也许这是UIFont的局限性吗? Have you tried using Core Text? 您是否尝试过使用Core Text?

According to iOS Core Text Fonts , "Core Text doesn't give you access to more fonts, but it provides support for a lot more font variations. To give an example: Helvetica Neue has 4 variations when you use UIFont whereas it has 11 variations when using Core Text." 根据iOS Core Text Fonts的说法,“ Core Text无法让您访问更多字体,但它提供了更多字体变体的支持。举个例子:当您使用UIFont时,Helvetica Neue有4种变体,而11种变体使用“核心文字”时。”

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

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