Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text overflow onto new line with WrappingHStack

I’m using WrappingHStack, and I want text elements within it (on buttons, see below) to overflow onto a new line. Currently, this does not happen — it gets truncated with ... at the end.

This does not happen if the button is outside the WrappingHStack:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Outside of WrappingHStack, text correctly overflows onto new line:

Button(action: {
    //
}) {
    VStack {
        Text("A very long sentence. Hopefully it will flow onto the next line")
            .font(.system(size: 18, weight: .bold, design: .rounded))
            .foregroundColor(.white)
    }
    .frame(alignment: .leading)
    .padding()
    .background(
        RoundedRectangle(cornerRadius: 6)
            .fill(colours[0])
    )
    .padding(5)
}
.padding(.bottom, 10)

Inside WrappingHStack, it doesn’t:

WrappingHStack(0..<sentences.count, id:\.self, alignment: .center) { i in
    
    Button(action: {
        //
    }) {
        VStack {
            Text("A very long sentence. Hopefully it will flow onto the next line")
                .font(.system(size: 18, weight: .bold, design: .rounded))
                .foregroundColor(.white)
        }
        .frame(alignment: .leading)
        .padding()
        .background(
            RoundedRectangle(cornerRadius: 6)
                .fill(colours[i])
        )
        .padding(5)
    }
    .padding(.bottom, 10)
    
}

How can I ensure text overflows in the WrappingHStack too?

>Solution :

Try the below answer:

add .fixedSize(horizontal: false, vertical: true)

VStack {
    Text("A very long sentence. Hopefully it will flow onto the next line")
        .font(.system(size: 18, weight: .bold, design: .rounded))
        .foregroundColor(.white)
        .fixedSize(horizontal: false, vertical: true)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading