In SwiftUI, how do lineLimit, truncationMode, minimumScaleFactor and multilineTextAlignment decide what a long merchant name shows in a transaction row?
answer
- Text wraps by default
- a closed range of lines
- head, middle or tail
- shrink before truncate
basics
~20 sText wraps without limit by default. lineLimit caps the lines, as a number or an iOS 16 range; truncationMode places the ellipsis; minimumScaleFactor lets text shrink before truncating; multilineTextAlignment aligns lines only within a multi-line Text.
solid answer
~40 sA `Text` with room to grow wraps onto as many lines as it needs. `lineLimit(1)` keeps one line and truncates the rest; `lineLimit(1...2)` (iOS 16) grows to two lines and then truncates; `lineLimit(2, reservesSpace: true)` keeps two lines' height even for a short name so rows align. `truncationMode(.middle)` keeps both ends of a long reference, and `.head` keeps the end. `minimumScaleFactor(0.8)` lets SwiftUI shrink the font to 80 % when the text does not fit, and it truncates if it still does not fit. `multilineTextAlignment(_:)` aligns lines inside a multi-line `Text`; on a single line it does nothing, because the Text is as wide as that line, so you align the view with its container's alignment or a frame instead.
code
swift · 24 linesimport SwiftUI
struct MerchantCell: View {
let merchant: String
let reference: String
let amount: String
var body: some View {
HStack(alignment: .firstTextBaseline) {
VStack(alignment: .leading) {
Text(merchant)
.lineLimit(1...2) // grow to two lines, then truncate
.multilineTextAlignment(.leading)
Text(reference)
.lineLimit(1)
.truncationMode(.middle) // keep both ends of the reference
}
Spacer()
Text(amount)
.lineLimit(1)
.minimumScaleFactor(0.8) // shrink to 80 % before truncating
}
}
}go deeper
Recall that Text wraps by default, that lineLimit caps the lines and that truncationMode picks where the ellipsis goes.
Explain the range and reservesSpace overloads from iOS 16, why multilineTextAlignment does nothing on one line, and how minimumScaleFactor shrinks before truncating.
Choose a fitting rule per field in a dense row, separate the Text's own rules from the stack's width allocation, and test them at the largest text sizes.
Set a team default for list rows, such as which fields may truncate and which may shrink, so that large text sizes degrade the same way across screens.
## What a Text does with too little room Apple's documentation says a text view always uses the space its content needs, and when a parent proposes less, it fits by **wrapping, tightening, scaling and truncation**. Out of the box there is no line limit, so a long merchant name in a narrow row simply takes more lines; it truncates only when it runs out of room, for example under a fixed height. Limiting lines, choosing where to cut and shrinking are opt-in through modifiers. How the row's stack divides width between the name and the amount (`fixedSize`, `layoutPriority`) is the lesson on size proposals; this one is about what the `Text` does with the width it gets. ## Limiting lines `lineLimit` has several overloads: | Modifier | Since | Effect on a Text | |---|---|---| | `lineLimit(1)` | iOS 13 | one line, truncate the rest | | `lineLimit(nil)` | iOS 13 | no limit, the default | | `lineLimit(1...2)` | iOS 16 | at least 1, at most 2 lines, then truncate | | `lineLimit(2...)` / `lineLimit(...3)` | iOS 16 | partial ranges | | `lineLimit(2, reservesSpace: true)` | iOS 16 | always occupies 2 lines of height | `reservesSpace` is the one that keeps a list tidy: a one-word merchant still takes two lines of height, so the amounts in every row line up. A range does the same job with two numbers: the lower bound is the fewest lines the view occupies, so `lineLimit(2...3)` keeps even a short name two lines tall, and the upper bound is where truncation starts. A vertical `TextField` grows between the bounds and then scrolls instead of truncating. ## Choosing where to cut `truncationMode(_:)` takes a `Text.TruncationMode`: - `.tail` (the usual result) keeps the beginning: "Corner Café Mark…". - `.middle` keeps both ends: useful for card references or IBAN-like strings whose last digits identify them. - `.head` keeps the end: "…Downtown Plaza". The ellipsis appears only when the text actually runs out of room. ## Shrinking before cutting `minimumScaleFactor(_:)` takes a fraction between 0 and 1. When the text does not fit, SwiftUI may draw it as small as that fraction of its font size; if it still does not fit at the minimum, it truncates. Two cautions: 1. It works with a constrained line count. A `Text` that may wrap freely will wrap rather than shrink. 2. Shrinking a user's chosen text size undoes Dynamic Type. Use it for a short value such as an amount, with a factor near 1, rather than for body text. `allowsTightening(true)` is the gentler cousin: it lets SwiftUI compress the space between characters when necessary to fit a line. ## Aligning lines `multilineTextAlignment(_:)` takes a `TextAlignment` (`.leading`, `.center`, `.trailing`) and sets it in the environment, so it reaches every text view below it. It aligns **lines relative to each other**. A single-line `Text` is exactly as wide as its line, so the modifier has no visible effect; to move a short name to the trailing edge, align its container or give it `frame(maxWidth: .infinity, alignment: .trailing)`. In `TextField` and `TextEditor` it applies even on one line, because their width is not set by their text. ## Putting it together for a row 1. Merchant: `lineLimit(1...2)` so most names show in full and outliers end with an ellipsis. 2. Reference: `lineLimit(1)` plus `truncationMode(.middle)` so the distinguishing tail survives. 3. Amount: `lineLimit(1)` plus `minimumScaleFactor(0.8)`, because a cut-off amount is worse than a slightly smaller one. 4. Test at the largest text sizes, where every one of these decisions shows. ## Working through a truncation bug report "Merchant names are cut off on small phones" is a common ticket. A useful order of questions: 1. **Is there a line limit at all?** Without one, the Text wraps; a cut-off name with no `lineLimit` usually means the height is constrained, for example by a fixed `frame(height:)` on the row. 2. **How wide is the Text actually offered?** If the amount or an icon takes most of the width, the fix is in the stack, not in the Text's modifiers. 3. **Is truncation the right outcome?** For a name, two lines via `lineLimit(1...2)` is often better than an ellipsis on one. 4. **Where should the cut fall?** `.middle` or `.head` when the end of the string carries the meaning. Answering these in order keeps the fix in the right place: the `Text`'s own modifiers for how it fits, the container for how much room it gets.
- Why does a merchant name with lineLimit(1) still truncate when the row looks wide enough?The `Text` truncates to whatever width its parent proposes, and in an `HStack` that width is shared with the amount and any spacer. Its own modifiers only decide how it fits; giving it more of the row is a layout question answered with `layoutPriority(_:)` or by fixing the amount's size.
- When would you use lineLimit with reservesSpace: true?When rows or cards must keep the same height whatever the text: a grid of merchant tiles, or a list where amounts should align. With `reservesSpace: true` a one-line name still occupies the height of the limit, so short and long names produce the same layout; without it the view shrinks to its content.
- A Text shows one line unless you allow more
- multilineTextAlignment(.trailing) moves a single-line Text to the right edge
- minimumScaleFactor makes text larger when there is extra room
- truncationMode(.head) keeps the beginning of the string
- minimumScaleFactor is a safe fix for any long body text