Is there a way in SwiftUI to detect if a user has Larger Text size enabled?

Viewed 1135

I've read all the articles about supporting dynamic text size, but my problem is that I have a view consisting of shapes and some text. I needed to hard code the height of this view, so when a user uses a larger text size the text overlaps the shapes. What I would like to do is detect when a larger text size is used and increase the hard coded height of the view.

1 Answers

There is an environment value for that:

@Environment(\.sizeCategory) var sizeCategory

With that, you can do stuff like:

if sizeCategory > ContentSizeCategory.large {
    // views for large text
} else {
    // views for regular/small text
}

You should also check out the @ScaledMetric property wrapper, which will auto-scale your var based on the user’s text size:

@ScaledMetric var height: CGFloat = 100

Here's a nice summary of both: https://swiftwithmajid.com/2019/10/09/dynamic-type-in-swiftui/

Related