London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
18.8 C
New York
Thursday, November 7, 2024

swift – iOS: Utilizing variable fonts in SwiftUI


I’m making an attempt to make use of Variable Fonts in my venture. This can be a new venture that’s focusing on iOS 18 and I’m testing it on simulator. Right here is how I’m loading and registering a variable font:

public enum VariableFont: String {
    case inter = "Inter"
    case geologica = "Geologica"
}

public enum GeologicaFontAxis: Int {
    case cursive = 258
    case shaerpness = 259
    case slant = 260
    case weight = 257
}

public extension Font {
    
    static func variableFont(
        _ font: VariableFont,
        dimension: CGFloat,
        axis: [Int: Int]
    ) -> Font {
        Font(UIFont(descriptor: .init(
            fontAttributes: [
                .name: font.rawValue,
                kCTFontVariationAttribute as UIFontDescriptor.AttributeName: axis
            ]
        ), dimension: dimension))
    }
    
    static func geologica(dimension: CGFloat, axis: [GeologicaFontAxis: Int]) -> Font {
        let tuples = axis.map({ (key: GeologicaFontAxis, worth: Int) in
            return (key.rawValue, worth)
        })
        let axis: [Int: Int] = .init(uniqueKeysWithValues: tuples)
        return variableFont(.geologica, dimension: dimension, axis: axis)
    }
    
    static func registerFont(_ fontName: String) {
        guard let fontURL = Bundle.module.url(forResource: fontName, withExtension: "ttf") else {
            print("Didn't find font in bundle")
            return
        }
        guard let fontDataProvider = CGDataProvider(url: fontURL as CFURL) else {
            print("Didn't create CGDataProvider from fontURL")
            return
        }
        guard let font = CGFont(fontDataProvider) else {
            print("Didn't create CGFont from knowledge supplier")
            return
        }
        if isDebugMode(), let variationAxes = font.variationAxes {
            let axes = variationAxes as NSArray
            for axe in axes {
                dump(axe)
            }
        }
        var error: Unmanaged<CFError>?
        guard CTFontManagerRegisterGraphicsFont(font, &error) else {
            print("Error registering font: (String(describing: error?.takeUnretainedValue()))")
            return
        }
        print("Font registered efficiently")
    }
}

I’ve optained Axis data for the font I’m utilizing, “Geologia”, utilizing fonttools:

<!-- Weight -->
<Axis>
  <AxisTag>wght</AxisTag>
  <Flags>0x0</Flags>
  <MinValue>100.0</MinValue>
  <DefaultValue>100.0</DefaultValue>
  <MaxValue>900.0</MaxValue>
  <AxisNameID>257</AxisNameID>
</Axis>

<!-- Cursive -->
<Axis>
  <AxisTag>CRSV</AxisTag>
  <Flags>0x0</Flags>
  <MinValue>0.0</MinValue>
  <DefaultValue>0.0</DefaultValue>
  <MaxValue>1.0</MaxValue>
  <AxisNameID>258</AxisNameID>
</Axis>

<!-- Sharpness -->
<Axis>
  <AxisTag>SHRP</AxisTag>
  <Flags>0x0</Flags>
  <MinValue>0.0</MinValue>
  <DefaultValue>0.0</DefaultValue>
  <MaxValue>100.0</MaxValue>
  <AxisNameID>259</AxisNameID>
</Axis>

<!-- Slant -->
<Axis>
  <AxisTag>slnt</AxisTag>
  <Flags>0x0</Flags>
  <MinValue>-12.0</MinValue>
  <DefaultValue>0.0</DefaultValue>
  <MaxValue>0.0</MaxValue>
  <AxisNameID>260</AxisNameID>
</Axis>

So that is the place the magic numbers in GeologicaFontAxis enum comes from. This appears working and I can see the “Font registered efficiently” log and axis knowledge once I run the app:

â–¿ 4 key/worth pairs #0
  â–¿ (2 parts)
    - key: kCGFontVariationAxisName #1
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: Weight #2
      - tremendous: NSString
        - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMinValue #3
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 100 #4
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMaxValue #5
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 900 #6
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisDefaultValue #7
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 100 #4
â–¿ 4 key/worth pairs #0
  â–¿ (2 parts)
    - key: kCGFontVariationAxisName #1
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: Cursive #2
      - tremendous: NSString
        - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMinValue #3
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 0 #4
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMaxValue #5
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 1 #6
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisDefaultValue #7
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 0 #4
â–¿ 4 key/worth pairs #0
  â–¿ (2 parts)
    - key: kCGFontVariationAxisName #1
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: Sharpness #2
      - tremendous: NSString
        - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMinValue #3
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 0 #4
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMaxValue #5
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 100 #6
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisDefaultValue #7
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 0 #4
â–¿ 4 key/worth pairs #0
  â–¿ (2 parts)
    - key: kCGFontVariationAxisName #1
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: Slant #2
      - tremendous: NSString
        - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMinValue #3
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: -12 #4
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisMaxValue #5
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 0 #6
      - tremendous: NSNumber
        - tremendous: NSValue
          - tremendous: NSObject
  â–¿ (2 parts)
    - key: kCGFontVariationAxisDefaultValue #7
      - tremendous: __NSCFString
        - tremendous: NSMutableString
          - tremendous: NSString
            - tremendous: NSObject
    - worth: 0 #6
Font registered efficiently

The “Geologica” has 4 axis and logs present this appropriately. And right here how I take advantage of it in SwiftUI:

let titleFont = Font.geologica(
    dimension: 24,
    axis: [GeologicaFontAxis.weight: 600]
)

Textual content("modern-airlines", bundle: .module)
    .font(titleFont)
    .textCase(.uppercase)
    .padding(.high, 12)

However it’s not working each on simulator or previews. How can I make it work? Can this be a simulator and preview bug?

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com