In a SwiftUI HStack, how do you align labels of different font sizes on their baselines, and what happens to a view without text?
answer
- where the letters sit
- first line versus last line
- no text falls back to an edge
- SF Symbols carry baselines
basics
~20 sPass .firstTextBaseline (or .lastTextBaseline) as the HStack's alignment so every child's first (or last) line of text shares one baseline. A view with no text aligns by its bottom edge instead, so custom images need an alignmentGuide or an SF Symbol.
solid answer
~40 sCreate the row with `HStack(alignment: .firstTextBaseline)`. SwiftUI then aligns the baseline of each child's top-most line of text, so a `.headline` label and a `.title` amount sit on one line even though their frames differ in height; `.lastTextBaseline` uses the bottom-most line instead, which suits values that belong on the last line of wrapped text. Baselines ignore descenders, and a nested `VStack` contributes its first or last line. A view that contains no text gets the equivalent of bottom alignment, so a custom asset image rests its bottom edge on the baseline. Fix that with an SF Symbol, which carries text baseline guides and follows `font(_:)`, or with `alignmentGuide(.firstTextBaseline) { d in d[.bottom] - 0.12 * d.height }`, which only works while `.firstTextBaseline` is the stack's alignment.
code
swift · 17 linesstruct ReceiptTotalRow: View {
let total: Decimal
var body: some View {
HStack(alignment: .firstTextBaseline) {
Image("store-logo")
.alignmentGuide(.firstTextBaseline) { d in
d[.bottom] - 0.12 * d.height
}
Text("Total")
.font(.headline)
Spacer()
Text(total, format: .currency(code: "EUR"))
.font(.title)
}
}
}go deeper
Recall that HStack(alignment: .firstTextBaseline) lines text up by where the letters sit, and that .center is the default you are replacing.
Explain the first and last baseline guides, the bottom fallback for views without text, and why SF Symbols align without extra work.
Diagnose a row that looks off at large Dynamic Type sizes or with custom artwork, and fix it with the matching guide rather than offsets or fixed frames.
Push baseline alignment and SF Symbols into shared row components so every team's forms and lists line up the same way without per-screen nudging.
## Why center alignment looks wrong for text An `HStack` defaults to `.center`, which lines up the vertical middle of every child. That is fine for equal-sized views, but it breaks as soon as text of different sizes sits side by side. On a receipt's total row, `Text("Total")` in `.headline` and the amount in `.title` have different heights, so centering them puts their **baselines**, the lines the letters sit on, at different heights. The eye reads that as a misaligned row. ## The baseline guides `VerticalAlignment` has two text guides, and `Alignment` combines them with horizontal guides for containers that align on both axes. | Guide | Lines up | Typical use | |---|---|---| | `.firstTextBaseline` | the baseline of each child's top-most line of text | icon plus a title that may wrap; mixed font sizes in a row | | `.lastTextBaseline` | the baseline of each child's bottom-most line | a value that should sit on the last line of a wrapped description | | `.leadingFirstTextBaseline`, `.centerLastTextBaseline` and the other composites | a horizontal guide plus a baseline guide | `ZStack`, `overlay(alignment:content:)` and `background(alignment:content:)` | Two details from Apple's `VerticalAlignment` documentation matter: - Baseline alignment **excludes descenders**, the tails of letters such as g and j, so the line is where the letters sit, not the bottom of the text frame. - Apple's `Alignment` documentation shows that for a container the first baseline is the top-most line of text inside it and the last baseline the bottom-most, so a nested `VStack` takes part in baseline alignment through its texts. ## Multi-line text in a settings row A settings row often has an icon beside a title and a description that wraps. With `.center` the icon floats beside the middle of the whole block. With `.firstTextBaseline` it sits on the first line, where readers expect it, and the row stays tidy however many lines the description takes at larger Dynamic Type sizes. `.lastTextBaseline` is the choice when something, such as a value or a chevron, belongs on the final line instead. ## Views that contain no text Baseline alignment needs a baseline. Apple's documentation states the rule: if you use a text baseline alignment on a view that contains no text, SwiftUI applies the equivalent of bottom alignment. So a custom image from the asset catalog sits with its **bottom edge on the baseline**, which usually looks too high next to the text. There are two fixes: 1. **Use an SF Symbol.** `Image(systemName:)` symbols carry text baseline guides, follow the `font(_:)` you apply and therefore line up with text of any size. 2. **Adjust the guide.** `alignmentGuide(.firstTextBaseline) { d in d[.bottom] - 0.12 * d.height }` tells the stack that this image's baseline is a little above its bottom edge, so the stack lowers the image to put that point on the line. The guide you override must be the stack's active alignment; Apple warns that otherwise SwiftUI doesn't invoke the closure. ## Checking the result Baseline problems hide at the default text size and show up later, so check them on purpose: 1. Preview the row at a small and at one of the largest Dynamic Type sizes; a row aligned by `.center` drifts visibly as the fonts grow at different rates. 2. Add a temporary `.border(.red)` to each child to see its frame; with baseline alignment the frames sit at different heights while the letters share a line, which is the intended result. 3. Preview the longest localized strings, because a label that wraps changes which line `.lastTextBaseline` uses. 4. Try the row in a right-to-left layout direction; baselines are vertical, so they hold, but the horizontal order of icon and label flips. ## A receipt total row ```swift HStack(alignment: .firstTextBaseline) { Image(systemName: "creditcard") Text("Total") .font(.headline) Spacer() Text(total, format: .currency(code: "EUR")) .font(.title) } ``` Every child, including the symbol, now meets on the first baseline, so the small label and the large amount read as one line. ## Checklist - Use `.firstTextBaseline` for any row that mixes font sizes or holds text that may wrap. - Prefer SF Symbols beside text; reach for `alignmentGuide` only for custom artwork. - Match the overridden guide to the stack's `alignment`, or the override is never used. - Aligning baselines across both rows and columns is a grid's job, covered with SwiftUI grids.
- Why does an asset-catalog image look too high in an HStack aligned on .firstTextBaseline?It contains no text, so SwiftUI applies the equivalent of bottom alignment: the image's bottom edge rests on the text baseline and the whole picture sits above the line. Override its guide with `alignmentGuide(.firstTextBaseline)` returning a point above `d[.bottom]`, or use an SF Symbol, which carries real text baseline guides.
- In a ZStack or overlay, how do you align text by its baseline on the vertical axis?Use a composite `Alignment` such as `.leadingFirstTextBaseline` or `.trailingLastTextBaseline`, which pairs a horizontal guide with a baseline guide. `ZStack(alignment:)`, `overlay(alignment:content:)` and `background(alignment:content:)` all accept these, so a caption can sit on the first baseline of the text it decorates.
- Center alignment lines up text baselines when font sizes differ.
- firstTextBaseline aligns the top edges of the text views.
- A view with no text is simply skipped by baseline alignment.
- An alignmentGuide for firstTextBaseline works even when the stack aligns by center.
- SF Symbols need the same manual baseline fix as custom asset images.