I am trying to build a text editor that shrinks to its content size. The closest I have been able to get has been to add the .scrollDisabled(true)
and .fixedSize(horizontal: false, vertical: true)
modifiers.
This almost achieves what I need. There are two problems though:
- long single line text gets cut off at the end
- creating line breaks causes the text editor to grow vertically as expected (uncovering the cut off text in point 1 above). However, when you delete the line breaks, the TextEditor does not shrink again.
I have had a radar open for some time: FB13292506. Hopefully opening a thread here will get more visibility.
And here is some sample code to easily reproduce the issue:
import SwiftUI
struct ContentView: View {
@State var text = "[This is some long text that will be cut off at the end of the text editor]"
var body: some View {
TextEditor(text: $text)
.scrollDisabled(true)
.fixedSize(horizontal: false, vertical: true)
}
}
#Preview {
ContentView()
}
Here is a gif of the behavior: