从UIFontDescriptor创建UIFont时忽略UIFontWeightTrait和UIFontDescriptorFamilyAttribute

jar*_*air 21 uifont ios7 uifontdescriptor uifontweighttrait

鉴于以下代码和运行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);
Run Code Online (Sandbox Code Playgroud)

我希望它的值是shouldBeAnUltraLightFontHelveticaNeue-UltraLight的一个实例,但它是:

<UICTFont: 0x908d160> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 24.00pt
Run Code Online (Sandbox Code Playgroud)

据我所知,我正在关注Apple文档.为什么完全忽略字体系列和字体粗细信息?

我试过的事情

无论这些更改如何,返回的字体始终是正常重量的Helvetica的香草实例.

Ell*_* P. 12

我遇到了同样的问题,文档没有多大帮助.最后我发现使用family属性结合了face属性:

 UIFontDescriptor* desc = [UIFontDescriptor fontDescriptorWithFontAttributes:
        @{
            UIFontDescriptorFamilyAttribute: @"Helvetica Neue",
            UIFontDescriptorFaceAttribute: @"Light"
        }
    ];
Run Code Online (Sandbox Code Playgroud)


rob*_*cer 7

来自文档:

字体特征字典键

以下常量可用作从其特征字典中检索有关字体描述符的信息的键.

NSString *const UIFontSymbolicTrait;
NSString *const UIFontWeightTrait;
NSString *const UIFontWidthTrait;
NSString *const UIFontSlantTrait;
Run Code Online (Sandbox Code Playgroud)

这对我来说就像这些键只是为了从字体描述符中获取信息而设计的- 而不是用于设置它.例如,您可以这样做:

UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
NSDictionary *traits = [font.fontDescriptor objectForKey:UIFontDescriptorTraitsAttribute];
CGFloat weight = [traits[UIFontWeightTrait] floatValue];
Run Code Online (Sandbox Code Playgroud)

这会告诉你,你得到的字体比正常体重稍微重一点.这似乎没有那么有用,因为能够要求更轻的字体而不必给出确切的名称 - 但它似乎是预期的用途.