动态字体与缩放字体
支持更大的文字,同时不破坏布局。
动态字体与缩放字体 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is Dynamic Type?
Dynamic Type lets users pick their preferred text size system-wide. Your app should respect it, not force one fixed size. 🔤
Use Text Styles
Pick semantic text styles like .title or .body instead of fixed point sizes. These scale automatically with the user's setting.
Text("Welcome")
.font(.title)
Text("Details here")
.font(.body)Why Not Fixed Sizes
A hard-coded .font(.system(size: 17)) ignores the user's choice and stays small. Text styles adapt; fixed numbers don't.
Text("Stuck small")
.font(.system(size: 17))Scaling Custom Fonts
Have a brand font? Use .font(.custom:size:relativeTo:) so it still scales with a chosen text style.
Text("Brand")
.font(.custom("Avenir", size: 17, relativeTo: .body))The ScaledMetric Wrapper
Need a spacing or image size to grow with text? @ScaledMetric scales any number alongside Dynamic Type.
@ScaledMetric var iconSize = 24
Image(systemName: "star").frame(width: iconSize)Let Text Wrap
Big text needs room. Avoid lineLimit(1) on important labels, or long words will be cut off with an ellipsis.
Text(longTitle)
.lineLimit(2)Allow Tighter Fitting
When space is truly tight, minimumScaleFactor lets text shrink a little before truncating, keeping it readable.
Text(price)
.minimumScaleFactor(0.7)Preview Large Sizes
Use dynamicTypeSize in a preview to simulate the largest setting and catch broken layouts early.
#Preview {
ContentView()
.dynamicTypeSize(.accessibility3)
}Cap the Range If Needed
You can limit how far text scales with dynamicTypeSize(...), but cap sparingly so you don't shut out users who need more.
VStack { }
.dynamicTypeSize(...DynamicTypeSize.accessibility1)Stacks Over Fixed Frames
Prefer flexible stacks over rigid heights. A fixed frame can clip large text, while a stack grows to fit.
Test at Both Ends
Check your app at the smallest and the largest Dynamic Type sizes. If both read well, most users in between will too.
Quick Check
You want a custom brand font to grow with the user's text size. What do you use?
Recap
You made text adapt: choose semantic text styles, scale custom fonts and metrics, let text wrap, and test the largest size. 👍
常见问题解答
「动态字体与缩放字体」课时是免费的吗?
是的 — 「动态字体与缩放字体」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。
「动态字体与缩放字体」这节课中我会学到什么?
支持更大的文字,同时不破坏布局。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 SwiftUI Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「动态字体与缩放字体」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 SwiftUI Academy 课中编写并运行代码吗?
能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。