In SwiftUI, why should a transaction row use font(.headline) or another text style rather than .system(size: 17), and how do custom fonts keep scaling?
answer
- Font is resolved late
- the user's text size setting
- a point size never moves
- custom(_:size:relativeTo:)
basics
~20 sA text style such as .headline resolves to a size that follows the user's Dynamic Type setting, while .system(size: 17) stays at 17 points. Custom fonts scale through Font.custom(_:size:), like body or like the style named in relativeTo:.
solid answer
~40 sIn SwiftUI a `Font` is a late-binding token: the system resolves it in the environment where the text draws. A text style such as `.headline`, `.body` or `.caption` resolves to a size that follows the user's Dynamic Type setting, so the row grows when someone picks larger text. `.system(size: 17)` and `Font.custom(_:fixedSize:)` resolve to the same point size at every setting. For a brand typeface, `Font.custom(_:size:)` takes the font's PostScript name and scales from the given size the way `body` scales; `custom(_:size:relativeTo:)` scales it like another style, such as `.footnote`. `font(_:)` sets the default font for the whole subtree through the environment, and a nearer `font(_:)` overrides it.
code
swift · 30 linesimport SwiftUI
struct TransactionRow: View {
let merchant: String
let category: String
var body: some View {
VStack(alignment: .leading, spacing: 2) {
Text(merchant)
.font(.headline) // a text style: scales with Dynamic Type
Text(category)
.font(.custom("AvenirNext-Regular", size: 13, relativeTo: .footnote))
Text(verbatim: "Ref 00427")
.font(.system(size: 11)) // a fixed point size: ignores Dynamic Type
}
}
}
struct StatementView: View {
let rows: [(merchant: String, category: String)]
var body: some View {
VStack(alignment: .leading) {
ForEach(rows.indices, id: \.self) { i in
TransactionRow(merchant: rows[i].merchant, category: rows[i].category)
}
}
.font(.subheadline) // the default for any Text below that sets no font
}
}go deeper
Recall the text styles by name, that they follow the user's text size, and that .system(size:) does not. Show font(_:) on a container setting a default for the Text views inside.
Explain Font as a token resolved in the environment, why a nearer font(:) wins, and how custom(:size:) and relativeTo: let a brand font scale while custom(_:fixedSize:) does not.
Review a screen for point sizes copied from a design file and replace them with styles plus weight or design modifiers; know the silent failures of custom fonts, such as a wrong PostScript name or a missing bold file.
Argue for one rule a team can enforce: text styles by default, custom fonts only through relativeTo:, and fixed sizes as reviewed exceptions, because an unreviewed fixed size is an accessibility defect that never shows at the default setting.
## What a Font is in SwiftUI A SwiftUI `Font` is not a loaded typeface with a fixed size. Apple's documentation calls it **a late-binding token**: the system resolves its value at the moment it draws text in a given environment. That is why the same `Font.headline` can come out at different sizes on two devices, or on one device before and after the user changes the text size in Settings. You apply a font with the `font(_:)` modifier. It **sets the default font for the view it modifies and everything inside it**, because the font travels down the hierarchy in the environment. A `font(_:)` placed closer to a `Text` overrides the one above it: - `.font(.subheadline)` on a `VStack` gives every `Text` inside that sets no font of its own the subheadline style. - `.font(.headline)` on one `Text` inside that stack wins for that view only. - A `Text` with no font anywhere above it uses the body style. ## Text styles and Dynamic Type **Dynamic Type** is the iOS setting that lets a user choose larger or smaller text. SwiftUI's built-in text styles follow it automatically: `largeTitle`, `title`, `title2`, `title3`, `headline`, `subheadline`, `body`, `callout`, `footnote`, `caption` and `caption2` (`extraLargeTitle` and `extraLargeTitle2` are visionOS-only and unavailable on iOS). Each style also carries a weight designed to sit beside the others, so a screen built from styles keeps its hierarchy when everything grows. | Font | Follows Dynamic Type | Typical use | |---|---|---| | `.headline`, `.body`, `.caption` … | yes | almost all app text | | `.system(.body, design: .rounded, weight: .semibold)` | yes | a text style with another design or weight (iOS 16 overload) | | `.custom("Name", size: 17)` | yes, scaled like `body` | a brand font at body rank | | `.custom("Name", size: 32, relativeTo: .title)` | yes, scaled like `title` | a brand font at another rank | | `.system(size: 17)` | no | content that must not grow | | `.custom("Name", fixedSize: 17)` | no | the same, for a custom font | The trap is the fixed row of that table. `.system(size: 17)` looks identical to `.body` at the default setting, so a screen built from point sizes passes every review on a designer's phone and then refuses to grow for a user who needs larger text. How spacing and images should grow alongside the text, and the accessibility sizes themselves, belong to the lesson on VoiceOver and Dynamic Type; the font choice is where it starts. ## Custom fonts that still scale Apple's article on applying custom fonts gives the path, and each step can fail quietly: 1. Add the font file to the app target. 2. List it under the `UIAppFonts` key in the app's `Info.plist`. 3. Ask for it by its **PostScript name**, which Font Book shows, not by the file name. 4. Use `Font.custom(_:size:)` so it scales relative to `body`, or `Font.custom(_:size:relativeTo:)` to scale it like another style. If SwiftUI cannot find the font it does not crash; the text draws in a fallback face, so a misspelt name shows up only as the wrong typeface on screen. Apple's documentation also notes that SwiftUI **does not synthesize bold or italic** for custom fonts: `weight(_:)` or `italic()` choose a weighted or italic variant only when the family ships one. ## Weight, design and width without losing the style You rarely need a point size to change how text looks. `fontWeight(_:)`, `fontDesign(_:)` and `fontWidth(_:)` adjust the current font, and `Font.system(_:design:weight:)` builds a text style with a design such as `.rounded` or `.monospaced`; all of these keep scaling. For a column of amounts, `monospacedDigit()` makes every digit the same width without leaving the style. ## Seeing the difference before users do A fixed size hides at the default setting, so look at the extremes on purpose: 1. Preview the screen with `.environment(\.dynamicTypeSize, .accessibility3)`, the same override Apple's custom-font article uses. 2. Compare a row built from text styles with one built from point sizes: the first grows, the second stays small beside it. 3. Check the custom font at the largest size too; a font requested with `fixedSize:` or a misspelt PostScript name both show up here. The result is usually a decision per piece of text rather than a blanket rule. Body text, titles and labels follow the styles. A handful of elements, such as a number inside a fixed-size badge or a decorative glyph, may justify a fixed size, and each one should be a reviewed exception rather than the habit of copying point sizes from a design file. ## Mistakes reviewers catch - A design file's "17 pt semibold" copied as `.system(size: 17, weight: .semibold)` instead of `.headline` or `.body.weight(.semibold)`. - A brand font requested with `.custom(_:fixedSize:)` because "the designer specified 15 pt". - `font(_:)` repeated on every `Text` when one modifier on the container would set the default. - Expecting `.bold()` to embolden a custom family that ships no bold file.
- How do you keep a text style but give it a rounded design or a heavier weight?Use `Font.system(.headline, design: .rounded, weight: .bold)` (that overload is iOS 16), or keep `.font(.headline)` and add `fontWeight(.bold)` or `fontDesign(.rounded)`. All of them still resolve from the text style, so the text keeps following Dynamic Type; only a point size or `fixedSize` would stop it.
- A designer's custom font looks right in body text but bold() does nothing on it. Why?SwiftUI does not synthesize bold or italic for custom fonts. `bold()`, `weight(_:)` and `italic()` can only select a variant the family actually ships, so the app has to bundle the bold file, list it in `UIAppFonts`, and either rely on the weight modifier finding it or request it by its own PostScript name.
- Every SwiftUI font scales with Dynamic Type, so system(size:) is as good as a text style
- Font.custom(_:size:) always stays at exactly the size you pass
- font(_:) styles only the one view it is attached to
- Calling bold() on any custom font synthesizes a bold face
- Font.custom needs the font's file name, extension included