Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the TextField Multiline - SwiftUI [duplicate]

I start learning SwiftUI and I'm trying to make the TextField multiline but it didn't work, also when I click on the return button it dismisses the keyboard.

TextField("Description", text: $categoryTitle)
.lineLimit(nil)

so how I can fix it?

like image 469
Adel Bassiony Avatar asked Nov 01 '25 14:11

Adel Bassiony


1 Answers

Since iOS 16

The lineLimit modifier works as expected if you choose .vertical value for the axis parameter. And it also supports range now:

TextField("Title", text: $text, axis: .vertical)
    .lineLimit(5...10)

Demo


Since iOS 14

Form iOS 14 and with Xcode 12, it's available as TextEditor

struct ContentView: View {
    @State var text: String = "Multiline \ntext \nis called \nTextEditor"

    var body: some View {
        TextEditor(text: $text)
    }
}

iOS 13

Also, if you want to support iOS 13, you can take at look this answer to port UITextField inside SwiftUI with full access to all of its properties.

like image 65
Mojtaba Hosseini Avatar answered Nov 04 '25 05:11

Mojtaba Hosseini