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)
}
}
}
Add .fixedSize(horizontal: false, vertical: true) — this worked for me in my use case.
of cource your answer is so good thank you
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.
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.
Excited to find this. There has been issues with the lineLimit() modifier for 6+ months now. Makes me wonder why we have not seen a fix in Xcode yet.
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.