Here’s how to create a feature highlights view in SwiftUI with icons, descriptions, and a grid layout using stacks:


import SwiftUI

struct FeatureHighlightView: View {
    let features: [Feature] = [
        Feature(icon: "wand.and.stars", title: "Magic Tools", description: "Powerful editing tools for creative expression."),
        Feature(icon: "person.3.fill", title: "Collaboration", description: "Work together with your team seamlessly."),
        Feature(icon: "bolt.fill", title: "Performance", description: "Optimized for speed and efficiency."),
        Feature(icon: "lock.fill", title: "Security", description: "Keep your data safe and secure."),
        // ... add more features
    ]
    
    let columns: [GridItem] = [
        GridItem(.flexible()),
        GridItem(.flexible()) // Two columns
    ]
    
    var body: some View {
        NavigationView(content: {
            ScrollView { // Make the content scrollable if needed
                LazyVGrid(columns: columns, spacing: 20) {
                    ForEach(features) { feature in
                        FeatureItemView(feature: feature)
                    }
                }
                .padding(.horizontal) // Add horizontal padding
            }
            .navigationTitle("Feature Highlights") // Add title
            .font(.largeTitle)
        })
    }
}

struct Feature: Identifiable { // Feature data model
    let id = UUID()
    let icon: String
    let title: String
    let description: String
}

struct FeatureItemView: View {
    let feature: Feature
    
    var body: some View {
        VStack(alignment: .leading, spacing: 10) {
            Image(systemName: feature.icon)
                .font(.largeTitle)
                .foregroundColor(.accentColor)
            
            Text(feature.title)
                .font(.headline)
            
            Text(feature.description)
                .font(.subheadline)
                .multilineTextAlignment(.leading) // Allow multiline text
        }
        .padding()
        .frame(maxWidth: .infinity) // Take up full width of the column
    }
}


#Preview {
    FeatureHighlightView()
}

Explanation:

  1. Feature Struct:
    • Represents a single feature with icontitle, and description properties.
  2. FeatureHighlightView:
    • features array stores the feature data.
    • columns defines a two-column grid layout.
    • ScrollView makes the view scrollable if the content exceeds the screen height.
    • LazyVGrid creates a grid layout that lazily loads items for better performance.
    • ForEach iterates over the features array, creating a FeatureItemView for each feature.
  3. FeatureItemView:
    • Displays a single feature item.
    • VStack arranges the icon, title, and description vertically.
    • Image(systemName:) displays the feature icon.
    • Text views display the title and description with appropriate fonts.
    • multilineTextAlignment(.leading) allows the description to wrap to multiple lines and aligns it to the left.
  4. Styling:
    • Padding is added around each feature item.
    • .frame(maxWidth: .infinity) ensures that each item takes up the full width of its column in the grid.
  5. Example Usage (ContentView):
    • Creates an instance of FeatureHighlightView.

Key Improvements:

  • Grid Layout: Uses LazyVGrid for an efficient and flexible grid layout.
  • Feature Data: Organizes feature data into a Feature struct.
  • Customizable: Easily add/modify features, icons, and descriptions.
  • Scrollable Content: Handles long lists of features with ScrollView.
  • Clear Presentation: Presents each feature with an icon, title, and multi-line description.