NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to make a TextField expand vertically as the user types

Paul Hudson    @twostraws   

Updated for Xcode 14.2

SwiftUI’s TextField has a single line by default, and TextEditor is a multiline alternative based on how much space you want to allocate. But there’s a middle ground: if we pass an axis parameter to TextField, we can ask it to start as a single line and grow upwards as the user types, optionally switching to scrolling once the text goes past a certain length.

For example, this creates a TextField that automatically expands as the user enters more and more text:

struct ContentView: View {
    @State private var bio = "About me"

    var body: some View {
        VStack {
            TextField("Describe yourself", text: $bio, axis: .vertical)
                .textFieldStyle(.roundedBorder)

            Text(bio)
        }
        .padding()
    }
}

Download this as an Xcode project

You can control how big the TextField can grow by adding a lineLimit() modifier. For example, we might want to say that it should start out as a single line, but allow growth up to a maximum of five lines:

struct ContentView: View {
    @State private var bio = "About me"

    var body: some View {
        VStack {
            TextField("Describe yourself", text: $bio, axis: .vertical)
                .textFieldStyle(.roundedBorder)
                .lineLimit(5)

            Text(bio)
        }
        .padding()
    }
}

Download this as an Xcode project

That doesn’t mean the user can’t type more than five lines, only that once the TextField goes beyond that limit it will switch to scrolling rather than growing further.

You can pass a range here if you want, for example using lineLimit(2...5) to mean “always be at least two lines high, but grow up to five.”

You can also use the reservesSpace parameter to lineLimit() so that the view automatically allocates enough space for the maximum size it can have. For example, this creates a TextField that reserves enough layout space to hold up to five lines of text:

struct ContentView: View {
    @State private var bio = "About me"

    var body: some View {
        VStack {
            TextField("Describe yourself", text: $bio, axis: .vertical)
                .textFieldStyle(.roundedBorder)
                .lineLimit(5, reservesSpace: true)

            Text(bio)
        }
        .padding()
    }
}

Download this as an Xcode project

The combination of this growing behavior and its ability to have a placeholder makes TextField a really great choice for entering user text, and preferable to TextEditor in situations where you aren’t relying on an exact view frame and you don’t need search and replace functionality.

Hacking with Swift is sponsored by Stream

SPONSORED Build a functional Twitter clone using APIs and SwiftUI with Stream's 7-part tutorial series. In just four days, learn how to create your own Twitter using Stream Chat, Algolia, 100ms, Mux, and RevenueCat.

Try now!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.