How do you create a multi-line text inside a ScrollView in SwiftUI?

后端 未结 4 2058
暖寄归人
暖寄归人 2020-11-30 02:12

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

相关标签:
4条回答
  • 2020-11-30 02:45

    The following works for me with Beta 3 - no spacer, no width constraint, flexible height constraint

    0 讨论(0)
  • 2020-11-30 02:58

    In Xcode 11 GM:

    For any Text view in a stack nested in a scrollview, use the .fixedSize(horizontal: false, vertical: true) workaround:

    ScrollView {
        VStack {
            Text(someString)
                .fixedSize(horizontal: false, vertical: true)
        }
    }
    

    This also works if there are multiple multiline texts:

    ScrollView {
        VStack {
            Text(someString)
                .fixedSize(horizontal: false, vertical: true)
            Text(anotherLongString)
                .fixedSize(horizontal: false, vertical: true)
        }
    }
    

    If the contents of your stack are dynamic, the same solution works:

    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)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 03:05

    You can force views to fill their ideal size, for example in a vertical ScrollView:

    ScrollView {
        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.")
            .fixedSize(horizontal: false, vertical: true)
    }
    

    Feels a little better to me than modifying the frame.

    0 讨论(0)
  • 2020-11-30 03:10

    It seems like there is bug in SwiftUI. For now you have to specify height for your VStack container

    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(nil)
                }.frame(width: UIScreen.main.bounds.width, height: 500)
           }
    
    0 讨论(0)
提交回复
热议问题