Text().lineLimit(n) Ignored Inside ScrollView

When we have a very long Text that needs more than one line to show its full contents, the text will get truncated with an ellipsis, if inside a ScrollView. Using .lineLimit(n) has no effect, for any value of n, including nil.

Bug Report: FB6859124

Workaround: unknown

Fixed On: iOS 13.1

Stackoverflow Question: https://stackoverflow.com/questions/56826050/how-to-get-dynamic-text-height-for-a-scrollview-with-swiftui/56827782#56827782

Sample Code

import SwiftUI

struct ContentView : View {
    var body: some View {
        ScrollView {
            Text("This is some very long text can we can see needs more than one line.")
                .lineLimit(10)
        }
    }
}

6 thoughts on “Text().lineLimit(n) Ignored Inside ScrollView”

  1. Thank you Vitaliy! That worked for me as well. What was interesting in my case is that a given Text() element was only intermittently truncated.

    My app has an inline help system, and Text() elements in this system were truncated if the user switched directly between help topics. If, instead, the user viewed a topic, dismissed the inline help, then viewed it again, the same Text() items that were previously truncated, were viewable again.

    Anyway, it’s great to have a workaround! I added your workaround to a ViewModifier so that it doesn’t need to be applied everywhere and that’s working as well.

    Reply
  2. The workaround causes other weird breakages, specially in complex views.

    It looks like the Text just really can’t calculate its intrinsic size when inside a scroll view. Any view I put under a Text this way overlaps its contents.

    Reply
  3. The workaround that I found works the best is to set the Text() attribute Text().fixedSize(vertical: true, horizontal: false)”. This way it makes the text expand all the way to the last line.

    Reply

Leave a Comment

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close