0

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.

2
  • Are you sure this is the performance bottleneck? I am almost certain that changing this will not make a noticeable difference in terms of efficiency... Commented Dec 20, 2022 at 6:11
  • That said, surely you would have found Font.Design.serif? Commented Dec 20, 2022 at 6:13

1 Answer 1

2

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

Text("Hello, world!")
    .font(.system(size: 20, design: .serif))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.