简体   繁体   English

文本未水平扩展 SwiftUI

[英]Text not expanding horizontally SwiftUI

Fetching text from JSON API all is working fine however text is not taking full space horizontally instead wrapping up after every item:从 JSON API 获取文本一切正常,但是文本没有水平占用全部空间,而是在每个项目之后包裹:

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. sampleName 来自嵌套数组。 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.我按照 SO 中的建议尝试了所有可能的组合,或者在其他地方没有任何效果,请参见图片。

I want like this:我想要这样:

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

Suggestions..建议.. 样品名称

This is normal behaviour of the Text view.这是Text视图的正常行为。 If you want to achieve what you want you can put everything into one single Text .如果你想实现你想要的,你可以把所有东西都放在一个Text中。 You would need to reduce your output into one single String .您需要将 output 减少为一个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
}

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM