1

I want the pills to break to the next line. As far I have no clue how to accomplish that.

Here is a bit of my code:

HStack {
    ForEach(interests, id: \.self) { interest in
        Text(interest)
            .padding(.horizontal, 10)
            .padding(.vertical, 5)
            .background(Color.blue)
            .foregroundColor(Color.white)
            .cornerRadius(25.0)
            .lineLimit(1)
            .fixedSize(horizontal: true, vertical: true)
    }
}

Here is the screenshot

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Timo
  • 11
  • 1

2 Answers2

0

If you're ok with setting a maximum # of items per row, here is an alternative solution. It basically loops through the interests and adds a new HStack after the itemsPerRow is reached. The numberOfRows can be greater than the actual number of rows shown (I included an if statement so that it will not create extra blank rows).

struct ContentView: View {
    
    let interests = ["One One", "Two Twooooooo", "ThreeThreeThree", "Four", "Five", "Six SIx Six SIx", "Seven", "Eight"]
    let itemsPerRow = 3
    let numberOfRows = 8
    
    var body: some View {
        VStack(alignment: .leading) {
            ForEach(0..<numberOfRows) { rowIndex in
                if interests.count >= (rowIndex * itemsPerRow) {
                    HStack {
                        ForEach(0..<interests.count) { index in
                            if
                                index < (rowIndex * itemsPerRow + itemsPerRow) &&
                                index >= (rowIndex * itemsPerRow)
                            {
                                Text(interests[index])
                                    .padding(.horizontal, 10)
                                    .padding(.vertical, 5)
                                    .background(Color.blue)
                                    .foregroundColor(Color.white)
                                    .cornerRadius(25.0)
                                    .lineLimit(1)
                                    .minimumScaleFactor(0.5)
                            }
                        }
                    }
                }
            }
        }
    }
}
nicksarno
  • 3,850
  • 1
  • 13
  • 33
0

New in iOS 14 is LazyVGrid which might help. Here is an example using your Interest pill.

struct WrappedPills: View {

let interests = [
    "Photographs",
    "Travel",
    "Reading",
    "Gardening",
    "Cooking",
    "Painting",
    "Dancing",
    "Music",
    "Hiking",
    "Art",
    "Astronomy",
    "Surfing",
    "Knitting",
]

//    let columns = [ GridItem(.adaptive(minimum: 100)) ]

let columns = [
    GridItem(.flexible()),
    GridItem(.flexible()),
    GridItem(.flexible()),
]

var body: some View {
    
    ScrollView() {
        HStack {
            Text("Interests")
                .font(.largeTitle)
                .fontWeight(.semibold)
                .padding(.top)
                .padding(.leading, 20)
            Spacer()
        }
        
        LazyVGrid(
            columns: columns,
            alignment: .leading,
            spacing: 10
        ){
            ForEach(interests, id: \.self) { interest in
                Text(interest)
                    .padding(.horizontal, 10)
                    .padding(.vertical, 5)
                    .background(Color.blue)
                    .foregroundColor(Color.white)
                    .cornerRadius(25.0)
                    .lineLimit(1)
                    .fixedSize(horizontal: true, vertical: true)
            } //ForEach
        } //LazyVGrid
    } //Scrollview
    .padding()

    }
}

You can try a few other options with the "columns" arrangement that might suit your needs better.

Paul Hudson has some good examples of LazyVGrid here: https://www.hackingwithswift.com/quick-start/swiftui/how-to-position-views-in-a-grid-using-lazyvgrid-and-lazyhgrid

enter image description here

Ryan
  • 10,798
  • 11
  • 46
  • 60