0

I need to create a table so that the text in the cell expands to the desired size. I create Content View:

struct ContentView: View {
    let items: [String] = [
        "1 One Line",
        "1 One Line",
        "1 One Line"
    ]
    var body: some View {
        ScrollView {
            VStack(spacing: 1) {
                ForEach(self.items, id: \.hash) { (item) in
                    HStack(spacing: 1) {
                        Text(item)
                            .frame(maxWidth: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(width: 70)
                            .background(Color.white)
                        Text("One Line")
                            .frame(width: 70)
                            .background(Color.white)
                    }
                }
                // MARK: Total
                HStack(spacing: 1) {
                    Text("Title")
                        .frame(maxWidth: .infinity, alignment: .center)
                        .background(Color.white)
                    Text("One Line")
                        .frame(width: 141)
                        .background(Color.white)
                }
            }
            .padding(1)
            .background(Color.orange)
        }
        .padding(15)
    }
}

and it works well: enter image description here But if the text becomes multi-line, then the table breaks:

let items: [String] = [
    "1 One Line",
    "2 Two Line\nTwo Line",
    "3 Three lines\nThree lines\nThree lines"
]

borders are getting wider: enter image description here

if you make maxHeight = infinity then this solves the problem with borders, but the lines become equal to the maximum height.

1 Answer 1

3

The possible solution is to use maxHeight: .infinity with .fixedSize

var body: some View {
        ScrollView {
            VStack(spacing: 1) {
                ForEach(self.items, id: \.hash) { (item) in
                    HStack(spacing: 1) {
                        Text(item)
                            .frame(maxWidth: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(maxHeight: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(maxHeight: .infinity, alignment: .center)
                            .background(Color.white)
                            
                    }
                    .fixedSize(horizontal: false, vertical: true) //<---Here
                }
                // MARK: Total
                HStack(spacing: 1) {
                    Text("Title")
                        .frame(maxWidth: .infinity, alignment: .center)
                        .background(Color.white)
                    Text("One Line")
                        .frame(width: 137)
                        .background(Color.white)
                }
            }
            .padding(1)
            .background(Color.orange)
        }
        .padding(15)
    }

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.