Multiline text input in SwiftUI (iOS 14)

As we know SwiftUI has been evolving since it was introduced. In iOS 13 when we want to create a text input field, it is done by TextField.

Textfield(text: $sometext)

In some cases we want to be able to provide a TextField that receives multiple lines. You may try something like this:

Textfield(text: $sometext).frame(minHeight: 200, maxHeight: .infinity, alignment: .topLeading)

Unfortunately this way you can only create a Textfield box that looks bigger, but when we actually type a long sentence the text won’t wraparound in the box. The old workaround used to be to make a MultilineTextview ourselves by manipulating UITextView from the old world of UIKit. Now in iOS 14, we have finally get the native way. Simply replace Textfield by Texteditor.

TextEditor(text: $sometext).frame(minHeight: 200, maxHeight: .infinity, alignment: .topLeading)

Now the text will wrap in the box. Give it a try!

The text wraps in the box

Leave a ReplyCancel reply