Skip to main content

How to specify the Dynamic Type sizes a view supports

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to specify the Dynamic Type sizes a view supports 관련

SwiftUI by Example

Back to Home

How to specify the Dynamic Type sizes a view supports | SwiftUI by Example

How to specify the Dynamic Type sizes a view supports

Updated for Xcode 15

SwiftUI’s automatic support of Dynamic Type means our views are free to grow and shrink according to the user’s preference. However, sometimes you might need to place limits on just how far your user interface is able to adjust, and for that we have the dynamicTypeSize() modifier.

This can used with a fixed value, meaning that your view will ignore all Dynamic Type sizes:

Text("This will stay small")
    .dynamicTypeSize(.xxLarge)

This is helpful if you want to force a particular kind of preview, for example.

You can also specify ranges, such as this one that allows any size up to and including the large size:

Text("This won't go above large")
    .dynamicTypeSize(...DynamicTypeSize.large)

If you enable Xcode’s environment overrides, you can see how each of these variants adjust:

VStack {
    Text("This will stay small")
        .dynamicTypeSize(.small)
    Text("This won't go above large")
        .dynamicTypeSize(...DynamicTypeSize.large)
    Text("This will scale within a range")
        .dynamicTypeSize(DynamicTypeSize.large...DynamicTypeSize.xxxLarge)
    Text("This will scale to any size")
}

Remember, many users rely on larger Dynamic Type sizes in order to use your app, and a surprisingly large number of users use it to cram more information onto their screen. So, try to avoid limiting the sizes you support to those times when it’s really necessary.

Similar solutions…
How to preview your layout at different Dynamic Type sizes | SwiftUI by Example

How to preview your layout at different Dynamic Type sizes
How to provide relative sizes using GeometryReader | SwiftUI by Example

How to provide relative sizes using GeometryReader
How to use Dynamic Type with a custom font | SwiftUI by Example

How to use Dynamic Type with a custom font
How to create a list of dynamic items | SwiftUI by Example

How to create a list of dynamic items
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks

이찬희 (MarkiiimarK)
Never Stop Learning.