Skip to content

⭐️⭐️⭐️⭐️⭐️ Fully customizable star ratings for iOS

License

Notifications You must be signed in to change notification settings

yonat/RatingControl

Repository files navigation

RatingControl

Swift Version License CocoaPods Compatible Platform PRs Welcome

⭐️ Fully customizable star ratings for iOS.

Features

  • Custom maximum rating value (default: 5)
  • Support for decimal rating values (e.g., 3.5 stars)
  • Custom images
  • Custom size
  • Rate using both tap and pan gestures

Usage

Basic Usage

Create a simple rating control with default settings:

let ratingControl = RatingControl()
ratingControl.value = 3.5 // Set initial rating

Customization

Customize appearance and behavior:

let ratingControl = RatingControl()
ratingControl.maxValue = 7 // 7 stars instead of default 5
ratingControl.value = 4.5
ratingControl.spacing = 8 // Increase spacing between stars
ratingControl.tintColor = .systemOrange // Change color
ratingControl.emptyImage = UIImage(systemName: "heart")! // Custom empty image
ratingControl.image = UIImage(systemName: "heart.fill")! // Custom filled image

User Interaction

Listen for rating changes:

ratingControl.addTarget(self, action: #selector(ratingChanged), for: .valueChanged)

@objc func ratingChanged(_ sender: RatingControl) {
    print("New rating: \(sender.value)")
}

Alternatively, disable user interaction:

ratingControl.isUserInteractionEnabled = false

SwiftUI Support

RatingView

Use the SwiftUI wrapper to integrate ratings in your SwiftUI views:

struct ContentView: View {
    @State private var rating: Double = 3.5
    
    var body: some View {
        VStack {
            // Basic usage
            RatingView(value: $rating)
            
            // Show current value
            Text("Rating: \(rating, specifier: "%.1f")")
            
            // Customization
            RatingView(value: $rating, maxValue: 7)
                .emptyImage(UIImage(systemName: "heart")!)
                .filledImage(UIImage(systemName: "heart.fill")!)
                .spacing(10)
                .imageSize(CGSize(width: 32, height: 32))
                .accentColor(.red)
                
            // Fixed (non-interactive) rating
            RatingView(value: 4.5)
                .disabled(true)
        }
    }
}

Available Modifiers

  • emptyImage(_:) - Sets the image for empty (unfilled) parts
  • filledImage(_:) - Sets the image for filled parts
  • spacing(_:) - Sets spacing between images
  • imageSize(_:) - Sets custom size for the images
  • Use SwiftUI's .accentColor(_:) to change the color
  • Use SwiftUI's .disabled(_:) to make it non-interactive

Installation

CocoaPods:

pod 'YSRatingControl'

Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/yonat/RatingControl", from: "1.0.0")]

About

⭐️⭐️⭐️⭐️⭐️ Fully customizable star ratings for iOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published