我正试图打破文本域和文本组件上的行,这可能吗?
已经尝试过使用frames和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 $0 { 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")
                }
            }

最佳答案

您可以对文本视图使用.lineLimit(nil)。它支持多行文本,并根据需要分配空间。

struct StackOverFlow : 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

关于swift - 如何在Swift UI的TextField和Text组件中放置多行(断行)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56949913/

10-10 20:52