0

Can we have a multiline text field in SwiftUI? I'm talking about something equivalent to Wrapping Text Field that is available for Cocoa. I hear that you can use TextEditor, an equivalent of NSTextView under macOS 11 and UITextView under iOS 14. No, that's not what I'm talking about. I'm talking about the NSTextField with Word Wrap as the line break.

enter image description here

struct EventView: View {
    @State var eventName: String = ""
    @State var eventSum: String = ""
    
    var body: some View {
        VStack {
            Spacer()
                .frame(height: 18)
            
            HStack(spacing: 0.0) {
                VStack {
                    HStack {
                        Spacer()
                            .frame(width: 16)
                        Text("Name: ")
                        TextField("", text: $eventName)
                        Spacer()
                            .frame(width: 16)
                    }
                    Spacer()
                        .frame(height: 10)
                    
                    HStack {
                        Spacer()
                            .frame(width: 16)
                        Text("Summary: ")
                        TextField("", text: $eventSum)
                            .frame(height: 48.0)
                            .lineLimit(4)
                            .multilineTextAlignment(.leading)
                        Spacer()
                            .frame(width: 16)
                    }
                    /*
                    Spacer()
                        .frame(height: 10)
                    HStack {
                        Spacer()
                            .frame(width: 16)
                        Text("Time: ")
                        
                    }
                    */
                }
            }
            Spacer()
            HStack(spacing: 0.0) {
                Button("Cancel") {
                    
                }
                .frame(width: 80.0)
                Spacer()
                    .frame(width: 20.0)
                Button("Save") {
                    
                }
                .frame(width: 80.0)
            }
            
            Spacer()
                .frame(height: 18)
        }
        .frame(width: 240, height: 180)
    }
}

If I run the code above, I only get a regular text field, which is equivalent to NSTextField with Clip as the line break. If I set the height to 48 points, it's been ignored. In case you need to know, my develoyment target is macOS 15, The Xcode version is 12.2. Thanks.

enter image description here

El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • Does this answer your question https://stackoverflow.com/a/63144255/12299030? (possible duplicate) – Asperi Dec 17 '20 at 08:50
  • Thanks, @Asperi. I'll take a close look at it after eating dinner. I'm not talking about `NSTextView` or `UITextView`, though. – El Tomato Dec 17 '20 at 08:58

0 Answers0