I have a single line TextField, and I'm looking to truncate excess text with an ellipsis (...) at the end.
I tried to use the truncationMode API, but it doesn't work at all, and it also completely messes up the TextField selection.
Here is the code to reproduce the problem on a brand new plain SwiftUI MacOS project: (running Monterey RC 1, Xcode 13.1 RC 1):
struct ContentView: View {
@State var text = ""
@State var text2 = ""
var body: some View {
NavigationView {
Text("Sidebar")
TextField("Placeholder", text: $text)
.font(.largeTitle)
.truncationMode(.tail)
TextField("Click on me to lose focus", text: $text2)
.frame(width: 100)
}
}
}
Run the app, and then type a bunch of text into the big text box. You should observe no trimming but also erratic behavior in the text box's selection.
I tried using a NSTextField with NSViewRepresentable, and it worked great, except for the fact that I'm also trying to center the text.
When the text is centered and NSTextField.lineBreakMode is set, then the cursor doesn't go to the right place when the placeholder is visible.
I was wondering if anybody is able to reproduce this issue, and, if so, are there any solutions?