Just figuring out how I can achieve multiple lines of text in a Text
. It seems like the Text
has the same default as UILabel
(one line), but I can't find any function which meets this criteria.
struct ContentView : View {
var body: some View {
VStack(alignment: .leading, spacing: 10) {
HStack {
Text("Avocado Toast").font(.system(size: 24))
}
// This Text does cut, and I wonder how I can achieve multiple rows
Text("Ingredients: Avocado, Almond Butter, Bread")
.font(.system(size: 20))
}
}
}
Edit
.lineLimit(X)
, did the trick. But is it possible to not set a specific amount, for instance. With just a 0?
Use
.lineLimit()
to limit the amount of lines of text. It takes an optional Int (Int?
) as an argument, and.lineLimit(nil)
allows unlimited lines.Edit: As of SwiftUI Beta 5,
Text
has a default line limit ofnil
, so text inText
will wrap by default.If lineLimit(nil) is not working for you, try setting layoutPriority manually to whatever it suits you
.layoutPriority(0.5)
Use this:
If that doesn't work, use this method:
For wrapping Text in a Form
.lineLimit(Int.max)
did not work for me. It seems there needs to be some width for it to know when to wrap. I believe the official way is with.fixedSize
:The documentation states:
My text kept truncating even with no line limit applied. Wrapping my content in a
ScrollView {}
solved it.See more info here: https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-horizontal-and-vertical-scrolling-using-scrollview