Home > Back-end >  System font of different sizes that respect Dynamic Type
System font of different sizes that respect Dynamic Type

Time:09-03

There are 2 ways I can find to generate Dynamic Type-sensitive sizing.

  1. System fonts
.font(.system(.title))
  1. Custom fonts
.font(.custom("AmericanTypewriter", size: 22, relativeTo: .title))

But let's say I want to use a system font that respects Dynamic type.

When I do something like this:

.font(.system(size: 28, weight: .regular, design: .rounded))

all Dynamic Type is lost.

How can I specify system fonts of a specific size, but let it scale with Dynamic Type?

CodePudding user response:

Get the scaled size:

let scaledSize = UIFontMetrics(forTextStyle: .body).scaledValue(for: 28)

and then use that to set your font size:

.font(.system(size: scaledSize, weight: .regular, design: .rounded))

You can replace .body in the UIFontMetrics initializer with any other UIFont.TextStyle

  • Related