# Solutions for: Star Rating

## Overview

### Subjects

## Sample Codes

### Example 1 - Hacking with Swift / 100 Days / Day-54

```swift
mport SwiftUI

struct RatingView: View {
    @Binding var rating: Int

    var label = ""
    var maximumRating = 5

    var offImage: Image?
    var onImage = Image(systemName: "star.fill")

    var offColor = Color.gray
    var onColor = Color.yellow

    var body: some View {
        HStack {
            if label.isEmpty == false {
                Text(label)
            }

            ForEach(1..<maximumRating + 1, id: \.self) { number in
                image(for: number)
                    .foregroundColor(number > rating ? offColor : onColor)
                    .onTapGesture {
                        rating = number
                    }
            }
        }
    }

    func image(for number: Int) -> Image {
        if number > rating {
            return offImage ?? onImage
        } else {
            return onImage
        }
    }
}

struct RatingView_Previews: PreviewProvider {
    static var previews: some View {
        RatingView(rating: .constant(4))
    }
}
```

![](https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FZA61SAeahpGI1KP486eD%2Fimage.png?alt=media\&token=32e0494a-b0f8-427e-bd14-bd3474d92067)

{% embed url="<https://youtu.be/t1E1on5F6po>" %}
[Hacking with Swift / 100 Days / Day-54 - Adding a custom star rating component](https://www.hackingwithswift.com/books/ios-swiftui/adding-a-custom-star-rating-component)
{% endembed %}

### Articles / Documents

*
