0

Fetching text from JSON API all is working fine however text is not taking full space horizontally instead wrapping up after every item:

HStack(alignment: .top){
    HStack{
        Text("Sample Type :")
            .font(.system(size: 12))
            .padding(.leading,18)
        
    }
    HStack(spacing: 4){
        ForEach(card.sampleType.indices){raw in
            Text("\(raw+1)." + "\(card.sampleType[raw].sampleName)")
                .font(.system(size:12))
                .fixedSize(horizontal: false, vertical: true)
        }
    }
}.foregroundColor(.blue)
                 

sampleName is coming from nested array. I need the numbering also so concatenated as shown in code. I tried all possible combo as suggested in SO or else where but nothing working see pic.

I want like this :

Sample Type : 1.Fluoride Plasma - F 2.EDTA Whole Blood 3.Serum 4. Fluoride 
              Plasma - F

Suggestions..sampleName

burnsi
  • 6,194
  • 13
  • 17
  • 27
tintin
  • 335
  • 2
  • 8

2 Answers2

0

This is normal behaviour of the Text view. If you want to achieve what you want you can put everything into one single Text. You would need to reduce your output into one single String.

var body: some View {
    HStack(alignment: .top){
        Text("Sample Type :")
            .font(.system(size: 12))
            .padding(.leading,18)
        
        Text(getNormalized())
            .font(.system(size: 12))
            .padding()
    }.foregroundColor(.blue)
}

func getNormalized() -> String{
    var result = ""
    
    for (index, card) in workoutTexts.enumerated(){
        result += "\(index+1)." + "\(card.sampleType[index].sampleName) "
    }
    
    return result
}
burnsi
  • 6,194
  • 13
  • 17
  • 27
0

Maybe it helps you:

HStack(alignment: .top) {
    HStack() {
        Text("Sample Type :")
            .font(.system(size: 12))
            .padding(.leading, 18)
        ScrollView(.horizontal) {
            HStack() {
                ForEach(card.sampleType.indices){raw in
                    Text("\(raw+1)." + "\(card.sampleType[raw].sampleName)")
                        .font(.system(size: 12))
                        .fixedSize(horizontal: true, vertical: false)
                }
            }
        }
    }
}.foregroundColor(.blue)
stosha
  • 2,108
  • 2
  • 27
  • 29