
Here’s a SwiftUI implementation of a product card using stacks to arrange the image, title, description, and price in a visually appealing way, along with explanations:
import SwiftUI
struct ProductCard: View {
let productImage: Image
let productName: String
let productDescription: String
let productPrice: String
var body: some View {
VStack(alignment: .leading) { // Main container, left-aligned
productImage // Top image
.resizable()
.scaledToFit()
.frame(height: 150) // Adjust image height as needed
VStack(alignment: .leading, spacing: 5) { // Content below image
Text(productName)
.font(.headline)
.fontWeight(.semibold) // Bold title
Text(productDescription)
.font(.subheadline)
.lineLimit(2) // Limit to 2 lines of description
HStack(alignment: .bottom) { // Price aligned to the bottom
Spacer() // Push price to the right
Text(productPrice)
.font(.headline)
.fontWeight(.bold) // Bold price
.padding(.top, 5) // Add a little top padding
}
}
.padding() // Padding around text content
}
.background(Color.white)
.cornerRadius(10) // Rounded corners
.shadow(radius: 3) // Subtle shadow effect
}
}
// Example Usage
struct ContentView: View {
var body: some View {
ProductCard(
productImage: Image("product_image"), // Replace with your image name
productName: "Awesome Product",
productDescription: "This is the best product you'll ever buy. It's amazing!",
productPrice: "$99.99"
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Explanation:
- ProductCard View:
- It takes four parameters:
productImage
,productName
,productDescription
, andproductPrice
. - The main
VStack
arranges the image and text content vertically with left alignment.
- It takes four parameters:
- Product Image:
productImage
is loaded and made resizable.scaledToFit()
ensures it fits the frame while maintaining aspect ratio.frame(height: 150)
sets the desired image height.
- Text Content:
- A nested
VStack
arranges the title, description, and price vertically. Text
views display theproductName
,productDescription
, andproductPrice
.- Different fonts (
headline
,subheadline
,bold
) are used for visual hierarchy. lineLimit(2)
limits the description to two lines.
- A nested
- Price Placement:
- An
HStack
is used to align the price to the bottom right corner. Spacer()
pushes the price to the right edge.padding(.top, 5)
adds a bit of top padding to the price.
- An
- Styling:
background(Color.white)
sets a white background.cornerRadius(10)
adds rounded corners to the card.shadow(radius: 3)
adds a subtle shadow effect.
Key Improvements:
- Clear Structure: Uses
VStack
andHStack
to create a well-organized and visually appealing layout. - Alignment and Spacing: Content is aligned and spaced appropriately using stack parameters and spacers.
- Image Handling: The product image is resized to fit within the frame while maintaining its aspect ratio.
- Typography: Different fonts and weights are used to create a visual hierarchy and highlight the product information.
- Visual Appeal: The card has a clean look with rounded corners and a subtle shadow.