Search code examples
iosswiftuifont

How to find the different keys for UIFontFeatureTypeIdentifierKey?


I'm using this great code from the New Font Features at WWDC 2015:

import UIKit

let pointSize : CGFloat = 60.0
let systemFontDesc = UIFont.systemFont(ofSize: pointSize,
                                             weight: UIFontWeightLight).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
    [
        UIFontDescriptorFeatureSettingsAttribute: [
            [
                UIFontFeatureTypeIdentifierKey: kFractionsType,
                UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector,
                ],
        ]
    ] )

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))

label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
label.text = "12/48"

The WWDC video shows other uses for UIFontDescriptorFeatureSettingsAttribute such as superiors (e = mc²)

But what are the keys to unlock the other attributes?

The documentation page for UIFontDescriptorFeatureSettingsAttribute just says that it's an array of dictionaries containing UIFontFeatureTypeIdentifierKey and UIFontFeatureSelectorIdentifierKey.

The documentation for those two keys (type and selector) just says they are NSNumbers.

For fractions, type is kFractionsType and selector is kDiagonalFractionsSelector

How can I find the keys for other available features?


Solution

  • Core Text will list the hidden features of a font for you:

    let desc = UIFontDescriptor(name: "Didot", size: 20) as CTFontDescriptor
    let f = CTFontCreateWithFontDescriptor(desc,0,nil)
    let arr = CTFontCopyFeatures(f)
    print(arr as Any)
    

    For features that have magic names, you'll have to look in the SFNTLayoutTypes.h header.