我已经阅读了所有关于支持动态文本大小的文章,但我的问题是我有一个由形状和一些文本组成的视图.我需要对此视图的高度进行硬编码,因此当用户使用较大的文本大小时,文本会与形状重叠.我想做的是检测何时使用较大的文本大小并增加视图的硬编码高度.
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这是两者的一个很好的总结:swiftwithmajid/2019/10/09/dynamic-type-in-swiftui/
Here's a nice summary of both: swiftwithmajid/2019/10/09/dynamic-type-in-swiftui/
更多推荐
SwiftUI 中是否有一种方法可以检测用户是否启用了更大的文本大小?
发布评论