Home > Enterprise >  SwiftUI Font - Is there a way to create Serif type Font without requiring UIFont to Font conversion?
SwiftUI Font - Is there a way to create Serif type Font without requiring UIFont to Font conversion?

Time:12-22

Currently, this is how I create Serif type SwiftUI font.

let textSize = 20

let sfFont = UIFont.systemFont(ofSize: textSize)

let fontDescriptor = sfFont.fontDescriptor

if let newYorkFontDescriptor = fontDescriptor.withDesign(.serif) {
    let newYorkFont = UIFont(descriptor: newYorkFontDescriptor, size: 0.0)

    return Font(newYorkFont)
} else {
    return Font(sfFont)
}

I was wondering, is there a way without having to perform UIFont to Font conversion, to improve code efficiency?

Thanks.

CodePudding user response:

You can achieve this using system(size:weight:design:) or system(_:design:weight:).

Text("Hello, world!")
    .font(.system(size: 20, design: .serif))
  • Related