In SwiftUI, when do you use a TextField with axis: .vertical instead of a TextEditor for a delivery-notes box that grows as the user types?
answer
- one grows, one scrolls
- placeholder support differs
- lineLimit with a range
- height, not content
- rich text arrived later
basics
~10 sUse TextField(_:text:prompt:axis: .vertical) with lineLimit(2...5) for a short note that should grow and keep a placeholder; use TextEditor for long-form or, from iOS 26, styled AttributedString text in a sized, scrollable area.
solid answer
~40 sA `TextField` with `axis: .vertical` (iOS 16) starts as one line and grows as text wraps, keeps its `prompt:` placeholder, `submitLabel` and focus, and looks like the other `Form` rows. `.lineLimit(2...5)` bounds its height; past the maximum it becomes scrollable, and it never limits what the string holds. A `TextEditor` is a scrollable long-form editing area: it has no `prompt:` parameter, so a placeholder is your own overlay, and you give it a height. Its default keyboard behaviour on scroll is interactive. From iOS 26 it can bind to an `AttributedString` with an `AttributedTextSelection` for formatted text. For a delivery note, choose the vertical text field.
code
swift · 24 linesimport SwiftUI
struct DeliveryNotes: View {
@State private var notes = ""
var body: some View {
Form {
TextField("Delivery notes", text: $notes,
prompt: Text("Gate code, safe place…"), axis: .vertical)
.lineLimit(2...5)
}
}
}
@available(iOS 26.0, *)
struct GiftMessageEditor: View {
@State private var message = AttributedString("Happy birthday!")
@State private var selection = AttributedTextSelection()
var body: some View {
TextEditor(text: $message, selection: $selection)
.frame(minHeight: 120)
}
}go deeper
Know both views exist: a TextField with axis: .vertical grows, and a TextEditor is a scrollable area for longer text.
Explain lineLimit ranges, why TextEditor needs its own placeholder and height, and which release added each initializer.
Review multiline input for layout surprises and availability: unframed editors in forms, and iOS 26 rich-text initializers without a fallback.
Decide when rich-text editing is worth an iOS 26 floor or a dual path, weighing feature value against the devices still on older releases.
## Two ways to take more than one line A checkout's **delivery-notes** box ("gate code, leave with a neighbour…") is usually short but sometimes runs to a few lines. SwiftUI offers two views for multiline input, and they behave differently enough that picking the wrong one shows up as a layout problem. - A **`TextField` with `axis: .vertical`** (iOS 16) starts as a single line and **grows as the text wraps**. - A **`TextEditor`** (iOS 14) is a **scrollable editing area** for long-form text, sized by its container and any frame you give it. ## The growing text field `TextField("Delivery notes", text: $notes, prompt: ..., axis: .vertical)` keeps everything a text field has: a **`prompt:`** placeholder, `submitLabel(_:)`, `onSubmit`, focus and the same look as the other rows of a `Form`. The **`lineLimit(_:)`** overload that takes a range (iOS 16) bounds its height: - `.lineLimit(2...5)` reserves room for at least two lines and grows up to five. - Apple's documentation says that when the text needs more than the limit, a vertical `TextField` **becomes scrollable**, whereas a `Text` view would truncate. - `.lineLimit(3, reservesSpace: true)` makes the field occupy three lines even when its text is shorter. None of these limits the **content**: the string can still hold more lines than are visible. ```swift import SwiftUI struct DeliveryNotes: View { @State private var notes = "" var body: some View { Form { TextField("Delivery notes", text: $notes, prompt: Text("Gate code, safe place…"), axis: .vertical) .lineLimit(2...5) } } } @available(iOS 26.0, *) struct GiftMessageEditor: View { @State private var message = AttributedString("Happy birthday!") @State private var selection = AttributedTextSelection() var body: some View { TextEditor(text: $message, selection: $selection) .frame(minHeight: 120) } } ``` ## The text editor `TextEditor(text:)` is for text that is the main job of the screen, such as a gift message or a review: 1. It takes **no `prompt:`**. Its initializers are `init(text:)`, `init(text:selection:)` (iOS 18, a `TextSelection` binding) and the iOS 26 `AttributedString` form, so a placeholder is something you draw yourself, for example in an overlay shown while the string is empty. 2. It is **scrollable** and is styled by environment modifiers such as `font(_:)`, `lineSpacing(_:)` and `multilineTextAlignment(_:)`. 3. Apple's documentation says its default keyboard behaviour on scroll is **interactive**, unlike other scrollable content. 4. From **iOS 26**, `TextEditor(text:selection:)` can bind to an **`AttributedString`** with an `AttributedTextSelection`, and then supports editing and formatting styled text; by default it shows the system formatting controls in the context menu and keyboard toolbar on iOS, which `textInputFormattingControlVisibility(_:for:)` (also iOS 26) can hide. ## Inside a Form Both views can sit in a `Form`, but they size differently there. The vertical text field behaves like any other row: its height follows its text, bounded by the line range, and its prompt lines up with the placeholders of the fields above it. A `TextEditor` is a scrolling area rather than a field that grows with its text, so a row containing one needs an explicit height, such as `.frame(minHeight: 120)`, to look intentional. When the app needs the cursor position or the selected range, for example to insert a template phrase where the user is typing, the `TextSelection` binding that both `TextEditor` and `TextField` accept from iOS 18 exposes it. ## Choosing | Need | `TextField(axis: .vertical)` | `TextEditor` | |---|---|---| | Placeholder | `prompt:` built in | Draw your own | | Starting height | One line, or the range's lower bound | Whatever frame you give it | | Growth | Grows to the `lineLimit` maximum, then scrolls | Fixed area that scrolls | | Fits a `Form` row | Yes, matches other fields | Needs an explicit height | | Rich text | No | iOS 26, with an `AttributedString` binding | For the delivery-notes box the vertical text field wins: it looks like its neighbours, shows a placeholder, and grows only when the user writes more. Reach for `TextEditor` when the text is long-form or styled. ## Mistakes reviewers catch - **A `TextEditor` dropped into a form row with no frame**, so its height is left to the container instead of being chosen. - **Expecting `lineLimit(2...5)` to stop input after five lines**; it only bounds the height. - **Using the iOS 26 `AttributedString` initializer** in an app that deploys to iOS 17 or 18 without an availability check; it does not exist on those releases.
- How do you give a TextEditor a placeholder?`TextEditor` has no `prompt:` parameter, so you draw one: overlay a `Text` with secondary styling, aligned to the top leading corner, and show it only while the bound string is empty. Disable hit testing on the overlay so taps still reach the editor.
- How do you use the AttributedString TextEditor in an app that still deploys to iOS 17?Mark the view that uses `TextEditor(text:selection:)` with an `AttributedString` binding as `@available(iOS 26.0, *)`, and choose it with `if #available(iOS 26.0, *)`, falling back to a `String`-bound editor on older releases. The initializer does not exist before iOS 26.
A vertical text field is a sticky note that unfolds as you write; a text editor is a notepad page of fixed size that you scroll.
- lineLimit(2...5) stops the user typing after five lines
- TextEditor takes a prompt: parameter like TextField does
- A vertical TextField truncates its text with an ellipsis past the line limit
- TextEditor has supported AttributedString editing since iOS 14
Assumes the iOS 27 SDK: TextField axis: and lineLimit ranges date from iOS 16, TextEditor's AttributedString binding from iOS 26.