SwiftUI 中是否有一种方法可以检测用户是否启用了更大的文本大小?

编程入门 行业动态 更新时间:2024-10-28 16:20:27
本文介绍了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.

推荐答案

有一个环境值:

@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 中是否有一种方法可以检测用户是否启用了更大的文本大小?

本文发布于:2023-11-06 15:55:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有一   更大   种方法   文本   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!