UIFontWeightTrait Ignored When Creating a New Font – TextKit Bug or a Jared Goof?

Given the following code and a device running iOS 7.1 or later:

 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);

I would expect the value of shouldBeAnUltraLightFont to be an instance of HelveticaNeue-UltraLight, but instead it is:

<UICTFont: 0x908d160> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 24.00pt

I am following the Apple documentation as far as I understand it. Why is the font family and font weight information completely ignored?

Things I’ve Tried

Here are some of the things I’ve tried:

Regardless of these changes, the font returned is always a vanilla instance of Helvetica at normal weight. The only exception is when I remove the UIFontDescriptorTraitsAttribute key/value pair from the attributes dictionary. This results in a 24-point, normal-weight instance of whatever font family I specified.

Things I Can’t Use

Why This Matters

A design calls for an ultra-light system font, which in practice should be an instance of Helvetica Neue UltraLight. I could manually specify something like this:

font = [UIFont fontWithName:@"HelveticaNeue-UltraLight"];

The problem with this approach is that it’s prone to errors. When iOS 7.0.3 was released, Apple quietly renamed all the Helvetica Neue italic font filenames, which broke a lot of apps that were manually specifying the old font names. So I need a solution that is resistant to future errors of this kind, but still allows me to get a specific weight of a system font.

I’ve cross-posted this to Stack Overflow if you have an answer.

|  23 Jul 2014