如何在 Swift UI 上的 TextField 和 Text 组件中放置多行(断行)?

How can I put multiple lines (breaking lines) in a TextField and Text components on Swift UI?

我正在尝试断开 TextField 和 Text 组件上的线条,这可能吗?

已经尝试使用框架和 VStack 但没有成功

我正在使用的文本,我希望第一个中断:

VStack (alignment: .leading){
            Text(message.message)
                .bold()
                .foregroundColor(Color.black)
            Text(self.dateString)
                .font(.caption)
                .foregroundColor(Color.gray)
        }

我正在使用的文本字段:

HStack {
                TextField(self.$messageModel.message, placeholder: Text("Write a message..."),
                          onEditingChanged: { if [=13=] { self.kGuardian.showField = 0 }
                })
                .background(GeometryGetter(rect: $kGuardian.rects[0]))
                Button(action: {
                    self.messageModel.to = self.user.uid
                    self.messageModel.status = "read"
                    self.messageModel.date = Date().timeIntervalSince1970
                    ChatService.sendMessage(message: self.messageModel)
                    self.messageModel.message = ""
                }) {
                    Text("Send")
                }
            }

尝试把.padding()放在VStack的按钮上

您可以在文本视图中使用 .lineLimit(nil)。它支持 multi-line 文本并根据需要分配 space。

struct Whosebug : View {
    var body: some View {
        Text("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
       .lineLimit(nil)
    }
}

Without linelimit

With linelimit