由于 List
目前看起来不可配置以删除行分隔符,因此我正在使用带有 VStack
的 ScrollView
> 在其中创建文本元素的垂直布局。示例如下:
ScrollView {
VStack {
// ...
Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis ullamcorper tortor, nec finibus sapien imperdiet non. Duis tristique eros eget ex consectetur laoreet.")
.lineLimit(0)
}.frame(width: UIScreen.main.bounds.width)
}
呈现的结果 Text
被截断为单行。在 ScrollView
之外,它呈现为多行。除了显式设置 Text
框架的高度外,我如何在 ScrollView
中实现这一点?
最佳答案
在Xcode 11 GM中:
对于嵌套在 ScrollView 中的堆栈中的任何 Text
View , 使用 .fixedSize(horizontal: false, vertical: true)
解决方法:
ScrollView {
VStack {
Text(someString)
.fixedSize(horizontal: false, vertical: true)
}
}
如果有多个多行文本,这也有效:
ScrollView {
VStack {
Text(someString)
.fixedSize(horizontal: false, vertical: true)
Text(anotherLongString)
.fixedSize(horizontal: false, vertical: true)
}
}
如果堆栈的内容是动态的,则同样的解决方案有效:
ScrollView {
VStack {
// Place a single empty / "" at the top of your stack.
// It will consume no vertical space.
Text("")
.fixedSize(horizontal: false, vertical: true)
ForEach(someArray) { someString in
Text(someString)
.fixedSize(horizontal: false, vertical: true)
}
}
}
关于swift - 如何在 SwiftUI 的 ScrollView 中创建多行文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56593120/