I am a Flutter developer learning SwiftUI. I am working on a ChatGPT clone app in SwiftUI and trying to recreate the expanding TextEditor
. I have tried many approaches mentioned in various articles, but all of them feel like a cheap workaround. The closest I have come is using a TextField
with the following method:
TextField("What's up?", text: $text, axis: .vertical)
.lineLimit(1...5)
However, I am unable to use the Enter
button to create a new line in the TextField
, which I believe is the reason TextEditor
exists.
This is the view I am trying to recreate:
You can stop the "return" key from dismissing the keyboard by adding the submitScope
modifier.
TextField("What's up?", text: $text, axis: .vertical)
.submitScope()
.lineLimit(1...5)
By default, tapping the "return" key creates a "submit" action and dismisses the keyboard, and you can handle this using onSubmit { ... }
. If you add .submitScope()
, there is no "submit" action, and so a new line is inserted.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments