Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable fonts in iOS 14 [closed]

The following code allows to create a font with different weights.

func makeFont(weight: CGFloat, size: CGFloat) -> UIFont {
    var attributesDict = [String: Any]()
    attributesDict["Weight"] = weight
    /* Rubik-Light - is a variable font */
    let fontDescriptor = UIFontDescriptor(
        fontAttributes: [
            UIFontDescriptor.AttributeName.name : "Rubik-Light",
            kCTFontVariationAttribute as UIFontDescriptor.AttributeName : attributesDict
        ]
    )
    return UIFont(descriptor: fontDescriptor, size: size)
}

It works fine on ios 13 and below, but doesn't work on iOS 14. Is there any solution?

like image 667
RPlay Avatar asked Oct 23 '25 09:10

RPlay


1 Answers

Solved. iOS 14 expects attribute ID instead of its name ("Weight").

So, attributeDict should look like this:

var attributesDict = [NSNumber: Any]()
attributesDict[NSNumber(value: 2003265652)] = weight

Attribute ID can be obtained as follows:

let variationAxes = (CTFontCopyVariationAxes(ctFont)! as Array)
like image 155
RPlay Avatar answered Oct 24 '25 23:10

RPlay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!