UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

TextEditor bug help.

Forums > SwiftUI

Hi All,

I have a bit of a strange or maybe even not so strange bug with making a dynamically expanding TextEditor feild in SwiftUI and I was wondering if anyone can help.

Basically, I have found a way of making a TextEditor feild look similar to a TextField as in a single line to start and then as you type it dynaically grows to any size needed using the following code:

TextEditor(text: $textBox)
            //.frame(height: 40) // Keeps constraint
            //.frame(minHeight: 40) // Causes to rise?
            //.frame(maxHeight: 200) // Not used for testing
            .frame(minHeight: 40, maxHeight: 500, alignment: .leading)
            .cornerRadius(20)
            .foregroundColor(Color.black)
            //.padding(.leading , 8)
            //.font(.body)
            //.lineLimit(1)
            .fixedSize(horizontal: false, vertical: true) // Keeps contained
            //.multilineTextAlignment(.leading)
            .overlay {
                RoundedRectangle(cornerRadius: 20)
                .stroke(Color.gray, lineWidth: 2) }

However, a bug has appeared that I am unable to work out. Basically, if you type or even paste content into the TextEditor made above all works fine and the TextEditor dynamically expands... great... this is just what I want, however if you hit the enter/return key at any point the TextEditor box shrinks and the contents get obscured until it then grows back to the maxHeight set when you type more. Can anybody help me work out what’s going on please? I have a feeling the minHeight property gets re-invoked when the enter key is pressed but I'm not able to find a way to text this as if I just set the height I get a regular single line TextEditor.

Thanks in advance!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.