08
Jan
2021
Star rating view in SwiftUI
Reading time: 1 min
This recipe shows how to implement a star rating view in SwiftUI.
The end result will look like this:
The view uses SF Symbols, allowing it to scale to any size and render in any color. It supports an arbitrary upper limit for the rating and automatically rounds to nearest half.
Here's the code:
struct StarsView: View {
private static let MAX_RATING: Float = 5 // Defines upper limit of the rating
private static let COLOR = Color.orange // The color of the stars
let rating: Float
private let fullCount: Int
private let emptyCount: Int
private let halfFullCount: Int
init(rating: Float) {
self.rating = rating
fullCount = Int(rating)
emptyCount = Int(StarsView.MAX_RATING - rating)
halfFullCount = (Float(fullCount + emptyCount) < StarsView.MAX_RATING) ? 1 : 0
}
var body: some View {
HStack {
ForEach(0..<fullCount) { _ in
self.fullStar
}
ForEach(0..<halfFullCount) { _ in
self.halfFullStar
}
ForEach(0..<emptyCount) { _ in
self.emptyStar
}
}
}
private var fullStar: some View {
Image(systemName: "star.fill").foregroundColor(StarsView.COLOR)
}
private var halfFullStar: some View {
Image(systemName: "star.lefthalf.fill").foregroundColor(StarsView.COLOR)
}
private var emptyStar: some View {
Image(systemName: "star").foregroundColor(StarsView.COLOR)
}
}
Then, just add it by providing a rating value:
VStack {
StarsView(rating: 3.5)
}
As of SF Symbols v.16, star.lefthalf.fill is now named star.leadinghalf.fill.