在文本中添加无限行 (SwiftUI) [英] Adding unlimited lines in a Text (SwiftUI)
问题描述
只是弄清楚如何在 Text
中实现多行文本.Text
似乎与 UILabel
具有相同的默认值(一行),但我找不到任何符合此条件的函数.
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))
}
}
}
编辑
.lineLimit(X)
,成功了.但是,例如,是否可以不设置特定数量.只有一个 0?
Edit
.lineLimit(X)
, did the trick. But is it possible to not set a specific amount, for instance. With just a 0?
推荐答案
用于在表单中包装文本 .lineLimit(Int.max)
对我不起作用.似乎需要一些宽度才能知道何时包装.我相信官方方法是 .fixedSize
:
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
:
Text(message)
.fixedSize(horizontal: false, vertical: true)
文档说明:
这个例子展示了 fixedSize(horizontal:vertical:) 对比其父视图更宽的文本视图的影响,保留了文本视图的理想的、未截断的宽度.
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(水平:垂直:)
这篇关于在文本中添加无限行 (SwiftUI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!