给定以下代码和运行iOS 7.1或更高版本的设备:
NSDictionary *fontTraitsDictionary = @{UIFontWeightTrait : @(-1.0)};
NSDictionary *attributesDictionary = @{
UIFontDescriptorFamilyAttribute : @"Helvetica Neue",
UIFontDescriptorTraitsAttribute : fontTraitsDictionary
};
UIFontDescriptor *ultraLightDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:attributesDictionary];
UIFont *shouldBeAnUltraLightFont = [UIFont fontWithDescriptor:ultraLightDescriptor size:24];
NSLog(@"%@", shouldBeAnUltraLightFont);
我期望shouldBeAnUltraLightFont
的值是HelveticaNeue-UltraLight的实例,但实际上它是:
<UICTFont: 0x908d160> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 24.00pt
据我理解,我遵循了苹果的文档。为什么字体系列和字重信息完全被忽略了?
我尝试过的方法
- 我尝试了其他像Helvetica、Avenir等家族名称。
- 我尝试了在有效范围从-1到1的其他字重,以0.25为增量。
无论做出这些更改,返回的字体总是普通字重的Helvetica的基本实例。
UIFontDescriptorTraitsAttribute
键,则生成的字体将是正确的字体族(“Helvetica Neue”,而不是“Helvetica”)。这可能是一个错误。更有可能的是,您应该使用fontDescriptorWithSymbolicTraits
来获取正确的字体,但是UIFontDescriptorSymbolicTraits
缺少轻/超轻的值。这可能是一个疏忽。 - blork<UICTFont:0x7b22c690> font-family:"Helvetica Neue";font-weight:normal;font-style:normal;font-size:24.00pt
),因此已经有了一些进展。 - Arek Holko<UICTFont:0x7bf91af0> font-family:“HelveticaNeue-UltraLight”;font-weight:normal;font-style:normal;font-size:16.00pt
。 - Nate Cook