I've just started using SwiftUI with the retail build. I cannot get one of my text views to automatically expand into multiple lines. I've read many thread on SO and HackingWithSwift but cannot get it to work. I think perhaps it might be tied to my other frames but I'm not sure where to start
struct Message: View {
var body: some View {
ZStack() {
Color.blue.cornerRadius(8)
VStack(alignment: .leading, spacing: 8) {
Text("Lorem Ipsum")
.foregroundColor(.white)
.bold()
.font(.system(size: 20))
Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Donec enim diam vulputate ut pharetra. Sed turpis tincidunt id aliquet risus feugiat in. Interdum velit laoreet id donec ultrices tincidunt arcu non. Lorem END")
.foregroundColor(.white)
.lineLimit(nil)
Text("Sent to Group1 by iShaymus")
.foregroundColor(.white)
.italic()
.opacity(0.5)
.font(.system(size: 12))
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.padding(12)
}
}
}
I've tried applying frames to the Text()
. I've tried adding .font(.body)
. I've tried setting .lineLimit(100)
. None of them worked. The output is always as follows:
The entire body string is noticeably longer:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Donec enim diam vulputate ut pharetra. Sed turpis tincidunt id aliquet risus feugiat in. Interdum velit laoreet id donec ultrices tincidunt arcu non. Lorem END
This is the working code thanks to Marc
import SwiftUI
struct Message: View {
@State var title = ""
@State var messageBody = ""
@State var sentBy = ""
@State var targetSite = ""
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(title)
.foregroundColor(.white)
.bold()
.font(.system(size: 20))
Text(messageBody)
.foregroundColor(.white)
.fixedSize(horizontal: false, vertical: true)
Text("Sent to \(targetSite) by \(sentBy)")
.foregroundColor(.white)
.italic()
.opacity(0.5)
.font(.system(size: 12))
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .topLeading)
.padding(12)
.background(Color.blue)
.cornerRadius(10)
}
}