只是弄清楚如何在 Text
中实现多行文本。似乎 Text
与 UILabel
(一行)具有相同的默认值,但我找不到满足此条件的任何函数。
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))
}
}
}
编辑
.lineLimit(X)
,成功了。但是,是否可以不设置特定数量,例如。只有一个 0?
最佳答案
在表单 .lineLimit(Int.max)
中包装文本对我不起作用。似乎需要一些宽度才能知道何时换行。我相信官方的方法是使用 .fixedSize
:
Text(message)
.fixedSize(horizontal: false, vertical: true)
文档指出:
This example shows the effect of fixedSize(horizontal:vertical:) on a text view that is wider than its parent, preserving the ideal, untruncated width of the text view.
https://developer.apple.com/documentation/swiftui/view/fixedsize(horizontal:vertical:)
关于ios - 在文本中添加无限行(SwiftUI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56482955/