4

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?

3
  • 2
    UITextField is one line only - Use UITextView for multiline Commented Oct 3, 2019 at 2:44
  • UITextView is pretty easy to make into a View. The main problem is that you can't use Color or Font. Commented Oct 3, 2019 at 11:11
  • See stackoverflow.com/questions/56471973/… for code. Commented Oct 3, 2019 at 17:35

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yea, finally Apple added it 🙏
@Mojtaba Hosseini This seems to break keyboard avoidance? Or is it just me?
1

iOS 16.0+ supprts multiline TextField.

struct ContentView: View { @State private var description: String = """ Join us, and let's force unwrap SwiftUl's birthday presents. Note that although this activity is optional, we may have guards at the entry. """ var body: some View { Form { TextField("Description", text: $description, axis: .vertical) .lineLimit(5) // You can restrict min & max visible lines } } } 

1 Comment

Yes, Finally 🎉

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.