0

I am trying to show all the properties of the Text component in a single view. Showing Text with different styles and fonts. For that I am adding multiple Text components in a single view.

To make it possible I am using the ScrollView component and adding all Text components in it. At some point, it won't allow me to add one more component in the scrollView and showing different errors each time.

Code:

struct TextClass: View {
    var body: some View {
        GeometryReader { geometry in
            ScrollView(.vertical, showsIndicators: false) {
                Text("This is suppose to be a really long text that can go on to multiple lines. By default, it could go more than one lines.").padding()

                Text("This is only one line regardless of how long the sentence is")
                    .padding()
                    .lineLimit(1)
                Text("Hello Swift - LargeTitle").font(.largeTitle).padding()
                Text("Hello Swift - Title").font(.title).padding()
                Text("Hello Swift - Headline").font(.headline).padding()
                Text("Hello Swift - SubHeadline").font(.subheadline).padding()
                Text("Hello Swift - body").font(.body).padding()
                Text("Hello Swift- callout").font(.callout).padding()
                Text("Hello Swift- Footnote").font(.footnote).padding()

                Text("Font Weight - Ultralight").fontWeight(.ultraLight).padding()
                Text("Font Weight - Thin").fontWeight(.thin).padding()
    //            Text("Font Weight - light").fontWeight(.light).padding()
    //            Text("Font Weight - Regular").fontWeight(.regular).padding()
            }.frame(width: geometry.size.width)
        }
    }
}

struct TextClass_Previews: PreviewProvider {
    static var previews: some View {
        TextClass()
    }
}

It shows error like:

enter image description here

If I comment line

Text("Font Weight - Thin").fontWeight(.thin).padding()

the error is gone.

It seems that it only allows me to add 10 Text component in this situation If I add more than that it gives me an error.

Is it a bug? is it a common behavior in SwiftUI? What is the benefit of scrollView if it won't allow adding more components?

2
  • It's documented limitation of SwiftUI, just embed your Text (or other views) in Group {} by 10 (or less) items Commented Jan 1, 2020 at 10:42
  • So sad, btw thanks for the information. Commented Jan 1, 2020 at 10:53

1 Answer 1

3

You need to use Group as you can't have more than 10 elements inside a view

struct TextClass: View {
    var body: some View {
        GeometryReader { geometry in
            ScrollView(.vertical, showsIndicators: false) {
                    Group {
                            Text("This is suppose to be a really long text that can go on to multiple lines. By default, it could go more than one lines.").padding()
                            Text("This is only one line regardless of how long the sentence is")
                                .padding()
                                .lineLimit(1)
                            Text("Hello Swift - LargeTitle").font(.largeTitle).padding()
                            Text("Hello Swift - Title").font(.title).padding()
                            Text("Hello Swift - Headline").font(.headline).padding()
                            Text("Hello Swift - SubHeadline").font(.subheadline).padding()
                            Text("Hello Swift - body").font(.body).padding()
                    }
                    Group {
                            Text("Font Weight - Ultralight").fontWeight(.ultraLight).padding()
                            Text("Font Weight - Thin").fontWeight(.thin).padding()
                            Text("Font Weight - light").fontWeight(.light).padding()
                            Text("Font Weight - Regular").fontWeight(.regular).padding()
                    }
            }.frame(width: geometry.size.width)
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was wondering why ScrollView couldn't have more than 10 views, the error I had was a compiler error extra arguments at positions #11, #12, #13 in cal, just to make this discoverable on Google. Thanks for the solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.