I am trying to represent a fraction with denominator larger than 9 in a SwiftUI Text.
I can implement this using individual elements and applying offsets but that get's a bit messy as the fractions change dynamically.
Is there a way to do this using attributedText?
I came across thi UIFont extension with deprecated methods and wondering if anything similar that can be used with SwiftUI:
extension UIFont {
    static func fractionFont(ofSize pointSize: CGFloat) -> UIFont {
        let systemFontDesc = UIFont.systemFont(ofSize: pointSize).fontDescriptor
        let fractionFontDesc = systemFontDesc.addingAttributes(
            [
                UIFontDescriptor.AttributeName.featureSettings: [
                    [
                        UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
                        UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
                    ], ]
            ] )
        return UIFont(descriptor: fractionFontDesc, size:pointSize)
    }
}
 
                        
UIFontis toll-free-bridged withCTFont, which means you can cast aUIFontto aCTFontby sayingas CTFont. And SwiftUI'sFonthas an initializer that takes aCTFont.So, using the
fractionFont(ofSize:)method you posted, this playground code:produces this result: