How to create light system font with special font attributes

Tags: ios uifont
By : Ricky

I need to create a UIFont which is:

  1. San Francisco
  2. Light weight
  3. Using the case insensitive font attribute (to display the '/' glyph vertically aligned to numbers)

I am using this code to go about it:

UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
NSArray *additionalFontSettings = @[@{UIFontFeatureTypeIdentifierKey: @(kCaseSensitiveLayoutType), UIFontFeatureSelectorIdentifierKey: @(kCaseSensitiveLayoutOnSelector)}];
fontDescriptor = [fontDescriptor fontDescriptorByAddingAttributes:@{UIFontDescriptorFeatureSettingsAttribute: additionalFontSettings, UIFontDescriptorTraitsAttribute: @{UIFontWeightTrait : @(UIFontWeightLight)}}];
return [UIFont fontWithDescriptor:fontDescriptor size:50];

I get the right font with the right feature enabled, but I can't get the font weight to be adhered to. I need the light weight of the system font. How do I achieve this?

By : Ricky


Turns out Matt was right with his hint. I had forgotten that you can derive a UIFontDescriptor from an existing UIFont.

Therefore, creating the descriptor like so works:

UIFont *lightFont = [UIFont systemFontOfSize:50 weight:UIFontWeightLight];
UIFontDescriptor *fontDescriptor = lightFont.fontDescriptor;
By : Ricky

This video can help you solving your question :)
By: admin