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

How to create multi-line editable text with TextEditor

Paul Hudson    @twostraws   

Updated for Xcode 14.0 beta 1

New in iOS 14

SwiftUI has a TextEditor view for handling multi-line, scrolling text. You can set the font, change the colors as needed, and even adjust line spacing and how many lines can be created.

You need to store the current value of your text field somewhere, using @State or similar. For example, we could create a text view to let the user enter profile data like this:

struct ContentView: View {
    @State private var profileText = "Enter your bio"

    var body: some View {
        NavigationView {
            TextEditor(text: $profileText)
                .foregroundColor(.secondary)
                .padding(.horizontal)
                .navigationTitle("About you")
        }
    }
}

Download this as an Xcode project

The line “Enter your bio”, followed by more more lines of text in the same area.

For single-line text entry, use TextField instead.

Tip: In case you were wondering, at this time there is no ability to add formatted text inside a TextEditor view.

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain. The code can be hard to write, full of edge cases, and time-consuming to maintain. RevenueCat provides a backend and wrapper around StoreKit to make things simple so you can focus on building your app.

Explore the docs

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: 4.3/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.