SwiftUI Expanding TextEditor

Rehmat Singh Gill

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:

ChatGPT iOS App's Expanding Text Editor

Sweeper

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.

edited at
0

Comments

0 comments
Login to comment

Related

TextEditor positioning in SwiftUI

TextEditor added / SwiftUi

Controlling size of TextEditor in SwiftUI

Transparent Background for TextEditor in SwiftUI

Updating a SwiftUI TextEditor on font change

Change background color of TextEditor in SwiftUI

SwiftUI replace and copy text TextEditor

Detecting keyboard "submit button" press for TextEditor SwiftUI

SwiftUI - Is there a way to create a read-only TextEditor?

Connect TextEditor text count to ProgressView in SwiftUI

How to add placeholder text to TextEditor in SwiftUI?

SwiftUI TextEditor: scrolling to top at text change

Show line numbers in macOS SwiftUI TextEditor

Changing TextEditor background color in SwiftUI for macOS

How to prevent TextEditor from scrolling in SwiftUI?

Disable selection cursor for TextEditor in macOS-SwiftUI

SwiftUI TextEditor disable editing but keep tapable

SwiftUI: expanding/shrinking List cells

SwiftUI stop Divider from expanding vertically in HStack

Text Label is not expanding according to string in ScrollView SwiftUI

Header view not expanding beyond safeArea SwiftUI

How to detect newlines (return) from a multiline string text in TextEditor SwiftUI?

macOS SwiftUI TextEditor keyboard shortcuts for copy, paste, & cut

SwiftUI - Prevent users from adding New Line (\n) in TextEditor

Populating SwiftUI List with array elements that can be editied in TextEditor

Dynamic row hight containing TextEditor inside a List in SwiftUI

Keeping text on TextEditor element while switching views SwiftUI

How can I get the string entered in the SwiftUI TextEditor and store it in an array?

SwiftUI: Prevent Image() from expanding view rect outside of screen bounds