SwiftUI 中是否有一种方法可以检测用户是否启用了更大的文本大小? [英] Is there a way in SwiftUI to detect if a user has Larger Text size enabled?
问题描述
我已经阅读了所有关于支持动态文本大小的文章,但我的问题是我有一个由形状和一些文本组成的视图.我需要对此视图的高度进行硬编码,因此当用户使用较大的文本大小时,文本会与形状重叠.我想做的是检测何时使用较大的文本大小并增加视图的硬编码高度.
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.
推荐答案
有一个环境值:
@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
}
您还应该查看 @ScaledMetric
属性包装器,它将根据用户的文本大小自动缩放您的变量:
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
这是两者的一个很好的总结:https://swiftwithmajid.com/2019/10/09/dynamic-type-in-swiftui/
Here's a nice summary of both: https://swiftwithmajid.com/2019/10/09/dynamic-type-in-swiftui/
这篇关于SwiftUI 中是否有一种方法可以检测用户是否启用了更大的文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!