Martin S
Martin S

Reputation: 386

How to use OpenType feature tags with UIFont?

How to use OpenType feature tags (eg. cv11) with UIFont in Swift?

Upvotes: 0

Views: 31

Answers (1)

Martin S
Martin S

Reputation: 386

It is quite simple:

func createFontWithOptionsUI(fontName: String, size: CGFloat) -> UIFont {
    let resultFont = UIFontDescriptor(name: fontName, size: size)
        .addingAttributes([
            .featureSettings: [
                "cv11",
                1
            ]
        ])

    // 0.0 means the size from the descriptor will prevail.
    return UIFont(descriptor: resultFont, size: 0.0)
}

For use with SwiftUI, you can just convert it to Font

Upvotes: 0

Related Questions