Search code examples
iosswiftfontsuifonttypography

Enabling typographic features in iOS system font for body text style


I’d like to modify the San Francisco font by enabling OpenType features it supports, such as one storey a or opened four and six. This setting should apply to body text style only.

Text style is set in Storyboard attributes inspector and not in code.

The code below, for some reason, isn’t working:

let bodyFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body)
// This isn’t working either: let bodyFontDescriptor = UIFont.preferredFont(forTextStyle: .body).fontDescriptor
let oneStoreyAFontDescriptor = bodyFontDescriptor.addingAttributes(
    [
        UIFontDescriptorFeatureSettingsAttribute:
        [
            [
                UIFontFeatureTypeIdentifierKey: kStylisticAlternativesType,
                UIFontFeatureSelectorIdentifierKey: kStylisticAltSevenOnSelector
            ]
        ]
    ]
)
textView.font = UIFont(descriptor: oneStoreyAFontDescriptor, size: 0.0)

What am I doing wrong? Generally, is this the right approach?


Solution

  • Looks like you need to play around selectors. After combining Six and Seven alternatives the following code have started working:

    let six = [UIFontFeatureTypeIdentifierKey: kStylisticAlternativesType,
                           UIFontFeatureSelectorIdentifierKey: kStylisticAltSixOnSelector]
    let seven = [UIFontFeatureTypeIdentifierKey: kStylisticAlternativesType,
                             UIFontFeatureSelectorIdentifierKey: kStylisticAltSevenOnSelector]
    let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body).addingAttributes([UIFontDescriptorFeatureSettingsAttribute: [six, seven]])
    let font = UIFont(descriptor: descriptor, size: 0.0)
    label.font = font
    

    enter image description here