代码之家  ›  专栏  ›  技术社区  ›  El Tomato

在SwiftUI中使用换行文本字段

  •  0
  • El Tomato  · 技术社区  · 4 年前

    我们可以在中有一个多行文本字段吗 SwiftUI ? 我说的是一种相当于“包装文本字段”的东西,可用于 可可 我听说你可以用 TextEditor ,相当于 NSTextView 在macOS 11和 UITextView iOS 14下。不,我不是在说这个。我说的是 NSTextField 使用Word Wrap作为换行符。

    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)
        }
    }
    

    如果我运行上面的代码,我只会得到一个常规的文本字段,相当于 NSTextField 使用Clip作为换行符。如果我将高度设置为48点,它将被忽略。如果你需要知道,我的开发目标是macOS 15,Xcode版本是12.2。谢谢。

    enter image description here

    0 回复  |  直到 4 年前