Paragraphs of Text is oddly truncated within ScrollView?

In a very basic app (iOS), I am finding that a view with this...


---


struct IdeaDetail: View {

var idea: Idea

var body: some View {

ScrollView() {

VStack(alignment: .leading) {

Text(verbatim: idea.name)

.font(.title)

Text(idea.text)

.lineLimit(nil)

.font(.body)

}

}

}

}


---


is truncating the text (idea.text). There appears to be no pattern to when it is truncating it. I want it to display the entire text (which is often many lines) but it is cutting off the last few lines in almost every case, even when it doesn't have to scroll. Putting this inside a scrolling element in UIKit hasn't been a problem for me in the past and I'm quite curious what's going on. I've checked the boundaries that are generated on the frame and they seem to move around quite a bit depending on the content of idea.text but they don't seem to want to grow to fit the entire contents of the string.


idea.name and idea.text are both String.


Thanks!

Replies

Every place I've had to deal with wrapping text, I've had to calculate the size of the view manually and then set a frame to that size, to keep the strange truncation behavior from occuring. I'd open a bug report on it, as I truly do believe it's a bug. The Text view just doesn't seem to know how much to set it's minimum size to (always seems to be about a half line off, and rounds unpredictably... though i'm guessing, since we have no real documentation on the underlying api). Searching the forums shows some pretty creative ways to work around it, but the most reliable I've found, is to use a TextView from the UIKit (or AppKit if you're on desktop) and ask it to calculate the size for you, then set a frame with a minimum height to that.

This appears to be a bug with SwiftUI. A work around that has worked for me, until this get fixed.

Attach the following modifier to any multi-line Text().

.fixedSize(horizontal: false, vertical: true)