如何在 SwiftUI 的 ScrollView 中创建多行文本? [英] How do you create a multi-line text inside a ScrollView in SwiftUI?

查看:30
本文介绍了如何在 SwiftUI 的 ScrollView 中创建多行文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 List 目前看起来不像是可配置的以删除行分隔符,我正在使用 ScrollViewVStack> 在其中创建文本元素的垂直布局.下面的例子:

Since List doesn't look like its configurable to remove the row dividers at the moment, I'm using a ScrollView with a VStack inside it to create a vertical layout of text elements. Example below:

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 中实现这一点?

The resulting Text rendered is truncated single-line. Outside of a ScrollView it renders as multi-line. How would I achieve this inside a ScrollView other than explicitly setting a height for the Text frame ?

推荐答案

Xcode 11 GM 中:

对于嵌套在滚动视图中的堆栈中的任何 Text 视图,使用 .fixedSize(horizo​​ntal: 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)
        }
    }
}

这篇关于如何在 SwiftUI 的 ScrollView 中创建多行文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆