Rating View (Half-Star Capable)
Dynamic 5-star rating view supporting full, half, and empty star states.
SwiftUI Implementation
Pure Vanilla Swift • No DependenciesratingView.swift
import Foundation
import SwiftUI
struct RatingView: View {
let rating: Double
var body: some View {
HStack(alignment: .lastTextBaseline, spacing: 3) {
ForEach(0..<5) { index in
if rating >= Double(index + 1) {
Image(systemName: "star.fill")
.foregroundStyle(.yellow)
} else if rating > Double(index) && rating < Double(index + 1) {
Image(systemName: "star.leadinghalf.filled")
.foregroundStyle(.yellow)
} else {
Image(systemName: "star")
.foregroundStyle(.gray)
}
}
Text(String(format: "%.1f", rating))
.padding(.leading, 5)
.foregroundStyle(.secondary)
}
}
}
struct UseRatingView: View {
var body: some View {
RatingView(rating: 4.5)
}
}
#Preview {
UseRatingView()
}Ready to copy and paste directly into any Xcode project running iOS 16.0+.
Recommended Use Cases
- First-run onboarding flows and feature introductions
- Paywalls, upgrade prompts, and subscription tiers
- Dashboard overviews, stats tracking, and metric highlights
Component Details
- Category
- Cards & Content Displays
- iOS Compatibility
- iOS 16.0+
- Xcode Version
- Xcode 15+
- Platform
- iOS
- License Tier
- Free
- Themes & Contexts
- dashboardonboardingpaywall
- Keywords
- ratingstarsreviewfeedback
C
Compot for iOS
Native SwiftUI playground app
Preview all 130+ components interactively on your device and copy code straight to Xcode with 1 tap.
Get Compot on the App StoreRelated SwiftUI Components
More components in Cards & Content Displays and related app architectures.
ButtonsiOS 16.0+
Primary Button Basic
High-emphasis prominent action button with standard SwiftUI prominence styling and haptic feedback.
View Component & Code
ButtonsiOS 16.0+
CTA Button Large Basic
Full-width, high-impact call-to-action button designed for onboarding screens, welcome flows, and paywalls.
View Component & Code
ButtonsiOS 16.0+
Secondary Button Basic
Medium-emphasis secondary action button styled for alternative user workflows and dismissal actions.
View Component & Code