首页 > 解决方案 > How to put a sentence in a every line of List in Swift?

问题描述

The UI structure of app looks like this enter image description here

struct SheetView: View {
var body: some View {
    List{
        Rectangle()
            .frame(width: 150, height: 5)
            .foregroundColor(.gray)
            .cornerRadius(10)
            .opacity(50)
        HStack{
            Image("logo")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 100)

            Text("How does diagnostic prediction works?")
                .font(.largeTitle)
                .fontWeight(.bold)
                .padding(25)

        }
        Text("This app tries to predict medical diagnosys by given symptoms. It curently works with the given symptoms:")


        // not a fancy way
        Text("abdominal pain")
        Text("abnormal menstruation")
        Text("acidity")
        Text("acute liver failure")
        Text("altered sensorium")
        Text("anxiety")

            .multilineTextAlignment(.leading)
            .lineSpacing(10)
        Spacer()
    }
    .padding(20)
}

}

I have a big list of symptoms ( text ) and I want to put every symptom in a List. I tried to stack it something like

Text("abdominal pain")
        Text("abnormal menstruation")
        Text("acidity")
        Text("acute liver failure")
        Text("altered sensorium")
        Text("anxiety")

but it's not a fancy way. What is the best practice to put a sentence in every line of List without stacking it using Text()?

标签: swiftswiftui

解决方案


If you have the Strings in an array using ForEach and have it return at Text with that element seems like the easiest way.


推荐阅读