Skip to content
Nutrition Facts preview
Rendered from the SwiftUI source on this page.

Nutrition Facts

From syxUI — written for both platforms, not translated between them.

A nutrition-facts panel with heavy rules and percent daily value.

Cards · SwiftUI · iOS 17

  • cards
  • swiftui

The actual source

TableNutrition.swift
// Nutrition Facts · syxUI · https://syxui.dev/components/table-nutrition
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

extension View {
    func tblCard(_ w: CGFloat) -> some View {
        self.frame(width: w)
            .background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 18, style: .continuous))
            .clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous))
            .overlay(RoundedRectangle(cornerRadius: 18, style: .continuous).stroke(Color.primary.opacity(0.08), lineWidth: 1))
            .shadow(color: .black.opacity(0.04), radius: 10, x: 0, y: 4)
    }
}

/// Nutrition-facts panel - heavy rules, calories, indented sub-nutrients, % daily value.
struct TableNutrition: View {
    let rows: [(String, String, String, Bool)] = [
        ("Total Fat", "8g", "10%", false), ("Saturated Fat", "1g", "5%", true),
        ("Cholesterol", "0mg", "0%", false), ("Sodium", "160mg", "7%", false),
        ("Total Carbs", "37g", "13%", false), ("Dietary Fiber", "4g", "14%", true),
        ("Protein", "3g", "", false),
    ]
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            Text("Nutrition Facts").font(.system(size: 18, weight: .heavy)).padding(.horizontal, 16).padding(.top, 12)
            HStack {
                Text("Serving size").font(.system(size: 12)).foregroundStyle(.secondary)
                Spacer()
                Text("1 bar (40g)").font(.system(size: 12, weight: .semibold))
            }.padding(.horizontal, 16).padding(.vertical, 6)
            Rectangle().fill(Color.primary).frame(height: 6).padding(.horizontal, 16).padding(.vertical, 4)
            HStack {
                Text("Calories").font(.system(size: 15, weight: .bold))
                Spacer()
                Text("190").font(.system(size: 22, weight: .heavy))
            }.padding(.horizontal, 16).padding(.vertical, 2)
            Rectangle().fill(Color.primary).frame(height: 2).padding(.horizontal, 16).padding(.vertical, 4)
            HStack { Spacer(); Text("% Daily Value").font(.system(size: 11, weight: .bold)) }.padding(.horizontal, 16)
            ForEach(Array(rows.enumerated()), id: \.offset) { _, r in
                VStack(spacing: 0) {
                    Divider().padding(.horizontal, 16)
                    HStack {
                        Text(r.0).font(.system(size: 13, weight: r.3 ? .regular : .semibold)).padding(.leading, r.3 ? 12 : 0)
                        Text(r.1).font(.system(size: 13)).foregroundStyle(.secondary)
                        Spacer()
                        Text(r.2).font(.system(size: 13, weight: .bold))
                    }.padding(.horizontal, 16).padding(.vertical, 6)
                }
            }
        }
        .padding(.bottom, 12)
        .tblCard(280)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17