Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swiftui

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.

like image 868
GarySabo Avatar asked Dec 29 '25 14:12

GarySabo


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/

like image 196
Adam Avatar answered Jan 01 '26 09:01

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!