【发布时间】:2014-09-14 15:27:43
【问题描述】:
给定以下代码和运行 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
据我所知,我正在关注 Apple 文档。为什么字体族和字体粗细信息被完全忽略了?
我尝试过的事情
- 我尝试过其他姓氏,例如 Helvetica、Avenir 等。
- 我尝试了valid range from -1 to 1 中的其他字体粗细,增量为 0.25
无论这些变化如何,返回的字体始终是正常粗细的 Helvetica 的香草实例。
【问题讨论】:
-
如果您从字典中删除
UIFontDescriptorTraitsAttribute键,则生成的字体属于正确的系列(“Helvetica Neue”,而不是“Helvetica”)。可能是一个错误。您似乎更有可能应该使用fontDescriptorWithSymbolicTraits来获得正确的字体,但UIFontDescriptorSymbolicTraits缺少light/ultralight 的值。可能是疏忽。 -
在 8.0b4 Helvetica Neue 返回 (
<UICTFont: 0x7b22c690> font-family: "Helvetica Neue"; font-weight: normal; font-style: normal; font-size: 24.00pt),所以有一些进展。 -
在我看来,您的代码没有任何问题,而是 iOS 中字体的实现。如果 Helvetica Neue 的每个变体实际上都是其自己的正常粗细字体,那么寻找轻量级变体将不会成功。这是 HelveticaNeue-UltraLight 的日志,按名称检索:
<UICTFont: 0x7bf91af0> font-family: "HelveticaNeue-UltraLight"; font-weight: normal; font-style: normal; font-size: 16.00pt。
标签: ios7 uifont uifontdescriptor uifontweighttrait