mikeTeixeira88
mikeTeixeira88

Reputation: 173

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

I'm trying to break the lines on TextField and Text components, is that possible?

Already tried to use frames and VStack but it didn't work

Text I'm using, I want the first one to break:

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

TextField I'm using:

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

Upvotes: 1

Views: 3992

Answers (2)

Rubaiyat Jahan Mumu
Rubaiyat Jahan Mumu

Reputation: 4127

You can use .lineLimit(nil) to your Text views. It supports multi-line text and allocates space as needed.

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

Upvotes: 3

Terry
Terry

Reputation: 108

Try to put .padding() at the button of VStack.

Upvotes: 0

Related Questions