### Setup Cosmos with Carthage (iOS 8+) Source: https://github.com/evgenyneu/cosmos/wiki/Swift-1.2-setup Integrate Cosmos using Carthage, a decentralized dependency manager. This method requires Carthage to be installed and is compatible with iOS 8 and later. It involves adding a line to your Cartfile and running carthage update. ```bash github "marketplacer/Cosmos" "swift-1" ``` -------------------------------- ### Setup Cosmos with CocoaPods (iOS 8+) Source: https://github.com/evgenyneu/cosmos/wiki/Swift-1.2-setup Integrate Cosmos using CocoaPods, a dependency manager for Swift and Objective-C projects. This method requires CocoaPods to be installed and is compatible with iOS 8 and later. It involves adding specific lines to your Podfile and running pod install. ```ruby use_frameworks! pod 'Cosmos', :git => 'https://github.com/marketplacer/Cosmos.git', :branch => 'swift-1' ``` -------------------------------- ### CosmosView: Basic Instantiation and Touch Callbacks (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Demonstrates how to instantiate CosmosView programmatically, set its rating and text, and configure touch callbacks for user input. It shows basic setup, frame-based instantiation, and custom settings application. ```swift import Cosmos // MARK: - Basic Instantiation let cosmosView = CosmosView() // Set the rating (typically 0-5) cosmosView.rating = 4.5 // Add optional text label (e.g., review count) cosmosView.text = "(1,234 reviews)" // MARK: - Programmatic Setup with Frame let ratingView = CosmosView(frame: CGRect(x: 20, y: 100, width: 200, height: 50)) ratingView.rating = 3.7 view.addSubview(ratingView) // MARK: - Setup with Custom Settings var settings = CosmosSettings() settings.starSize = 30 settings.fillMode = .half settings.filledColor = .systemYellow let customCosmosView = CosmosView(settings: settings) // MARK: - Touch Callbacks for User Input cosmosView.didTouchCosmos = { // Called continuously as user drags across stars print("User is selecting rating: \(rating)") } cosmosView.didFinishTouchingCosmos = { // Called when user lifts finger - ideal for saving to database print("Final rating selected: \(rating)") // saveRatingToServer(rating) } ``` -------------------------------- ### Add Cosmos Source File (iOS 7+) Source: https://github.com/evgenyneu/cosmos/wiki/Swift-1.2-setup This method involves directly adding the CosmosDistrib.swift source file to your Xcode project. It is compatible with iOS 7 and later. No external dependencies are required beyond Xcode. ```swift // Add CosmosDistrib.swift file directly to your Xcode project. // Compatible with iOS 7+. ``` -------------------------------- ### Storyboard Setup for CosmosView in Swift Source: https://context7.com/evgenyneu/cosmos/llms.txt This snippet details the steps to set up the CosmosView component directly within Interface Builder. It covers dragging the view, setting its class and module, and configuring various appearance and behavior properties through the Attributes Inspector. ```swift // MARK: - Storyboard Setup Steps // 1. Drag a UIView onto your storyboard // 2. Set the class to "CosmosView" in Identity Inspector // 3. Set the module to "Cosmos" (unless using single-file distribution) // 4. Configure appearance in Attributes Inspector: // - Rating: Initial rating value (e.g., 3.5) // - Total Stars: Number of stars (default: 5) // - Star Size: Size in points (default: 20) // - Star Margin: Space between stars (default: 5) // - Fill Mode: 0=full, 1=half, 2=precise // - Filled Color: Background of filled stars // - Empty Color: Background of empty stars // - Border colors and widths // - Text settings (color, size, margin) // - Update On Touch: Enable/disable user input // MARK: - Connecting Outlet class ReviewViewController: UIViewController { @IBOutlet weak var ratingView: CosmosView! override func viewDidLoad() { super.viewDidLoad() // Additional programmatic configuration if needed ratingView.didFinishTouchingCosmos = { [weak self] rating in self?.handleRatingChanged(rating) } } func handleRatingChanged(_ rating: Double) { print("User selected \(rating) stars") } } ``` -------------------------------- ### Customize Cosmos View Settings in Swift Source: https://github.com/evgenyneu/cosmos/blob/master/README.md Provides examples of customizing the appearance and behavior of the Cosmos view using its settings object. This includes disabling rating updates on touch, setting fill modes for stars, adjusting star size and margin, and defining colors for filled and empty stars, as well as their borders. ```swift // Do not change rating when touched // Use if you need just to show the stars without getting user's input cosmosView.settings.updateOnTouch = false // Show only fully filled stars cosmosView.settings.fillMode = .full // Other fill modes: .half, .precise // Change the size of the stars cosmosView.settings.starSize = 30 // Set the distance between stars cosmosView.settings.starMargin = 5 // Set the color of a filled star cosmosView.settings.filledColor = UIColor.orange // Set the border color of an empty star cosmosView.settings.emptyBorderColor = UIColor.orange // Set the border color of a filled star cosmosView.settings.filledBorderColor = UIColor.orange ``` -------------------------------- ### Supply Custom Star Images for Cosmos View in Swift Source: https://github.com/evgenyneu/cosmos/blob/master/README.md Illustrates how to replace the default star drawing with custom images for both filled and empty states. This requires having the specified image assets (e.g., "GoldStarFilled", "GoldStarEmpty") available in your project's asset catalog. ```swift // Set image for the filled star cosmosView.settings.filledImage = UIImage(named: "GoldStarFilled") // Set image for the empty star cosmosView.settings.emptyImage = UIImage(named: "GoldStarEmpty") ``` -------------------------------- ### Control Cosmos View Rating and Text in Swift Source: https://github.com/evgenyneu/cosmos/blob/master/README.md Demonstrates how to programmatically set the rating and text displayed by the Cosmos view. It also shows how to define closures for handling user interactions, specifically when the user finishes touching the view (for saving data) and when the rating is being changed by touch (for UI updates). ```swift // Change the cosmos view rating cosmosView.rating = 4 // Change the text cosmosView.text = "(123)" // Called when user finishes changing the rating by lifting the finger from the view. // This may be a good place to save the rating in the database or send to the server. cosmosView.didFinishTouchingCosmos = { rating in } // A closure that is called when user changes the rating by touching the view. // This can be used to update UI as the rating is being changed by moving a finger. cosmosView.didTouchCosmos = { rating in } ``` -------------------------------- ### CosmosSettings: Appearance and Behavior Configuration (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Explains how to configure CosmosView's appearance and behavior using the CosmosSettings struct. Covers star size, colors, margins, fill modes, text label settings, and touch behaviors. Settings can be applied during instantiation or to an existing view. ```swift import Cosmos // MARK: - Creating and Applying Custom Settings var settings = CosmosSettings() // Star appearance settings.starSize = 25 // Size of each star in points settings.totalStars = 5 // Number of stars to display settings.starMargin = 8 // Space between stars settings.fillMode = .precise // .full, .half, or .precise // Colors for filled stars settings.filledColor = UIColor.systemOrange settings.filledBorderColor = UIColor.systemOrange settings.filledBorderWidth = 1.0 // Colors for empty stars settings.emptyColor = UIColor.clear settings.emptyBorderColor = UIColor.systemOrange settings.emptyBorderWidth = 1.0 // Text label settings settings.textColor = UIColor.secondaryLabel settings.textFont = UIFont.systemFont(ofSize: 14, weight: .medium) settings.textMargin = 10 // Space between stars and text // Touch behavior settings.updateOnTouch = true // Allow user input settings.minTouchRating = 0 // Minimum selectable rating (default: 1) settings.passTouchesToSuperview = true // Pass touches to parent view settings.disablePanGestures = false // Set true for modal sheets on iOS 13+ // Apply settings to CosmosView let cosmosView = CosmosView(settings: settings) // Or apply to existing view cosmosView.settings = settings // MARK: - Reusing Settings Across Multiple Views var sharedSettings = CosmosSettings() sharedSettings.starSize = 20 sharedSettings.filledColor = .systemYellow let productRating = CosmosView(settings: sharedSettings) let reviewRating = CosmosView(settings: sharedSettings) ``` -------------------------------- ### Add Cosmos Source File (iOS 7+) Source: https://github.com/evgenyneu/cosmos/wiki/iOS-7-support This method allows direct integration of the Cosmos library by adding a single Swift file to your Xcode project. It is specifically designed for iOS 7 compatibility and requires Swift 2.2. ```swift import UIKit // CosmosDistrib.swift file content would go here ``` -------------------------------- ### Set Cosmos View Settings in Objective-C Source: https://github.com/evgenyneu/cosmos/wiki/Using-Cosmos-settings-in-Objective-C Demonstrates how to use the CosmosSettingsObjCBridge to configure various properties of a Cosmos view, such as star size, fill mode, filled color, and touch update behavior. This requires importing the Swift bridge file into your Objective-C code. ```objective-c #import "YOUR_PRODUCT_MODULE_NAME-Swift.h" - (void)viewDidLoad { [super viewDidLoad]; [CosmosSettingsObjCBridge setStarSize:30 inCosmosView:self.cosmosView]; [CosmosSettingsObjCBridge setFillMode: 0 inCosmosView: self.cosmosView]; [CosmosSettingsObjCBridge setFilledColor: [UIColor redColor] inCosmosView:self.cosmosView]; [CosmosSettingsObjCBridge setUpdateOnTouch: NO inCosmosView:self.cosmosView]; } ``` -------------------------------- ### Prepare Cosmos View for Reuse in Table View Cell (Swift) Source: https://github.com/evgenyneu/cosmos/wiki/Using-Cosmos-in-a-scroll-view This Swift code demonstrates how to properly prepare a `CosmosView` for reuse within a `UITableViewCell`. It ensures that any previous state of the `CosmosView` is reset before the cell is reused, preventing potential display issues. This is essential for maintaining performance and correct behavior in dynamic table views. ```swift public class PerformanceTableViewCell: UITableViewCell { @IBOutlet var cosmosView: CosmosView! override public func prepareForReuse() { // Ensures the reused cosmos view is as good as new cosmosView.prepareForReuse() } } ``` -------------------------------- ### Reuse Cosmos Settings in Swift Source: https://github.com/evgenyneu/cosmos/wiki/Cosmos-configuration Create and reuse a `CosmosSettings` object to configure multiple Cosmos views with identical appearance settings. This promotes consistency and simplifies management of shared configurations. ```swift var settings = CosmosSettings() settings.starSize = 142 var cosmos1 = CosmosView(settings: settings) var cosmos2 = CosmosView(settings: settings) ``` -------------------------------- ### Integrate Cosmos in Table View Cells (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Implement CosmosView within table view cells, ensuring proper reuse handling by calling `prepareForReuse()` on the CosmosView. This prevents stale data and ensures correct display. ```swift import UIKit import Cosmos // MARK: - Custom Table View Cell class ProductTableViewCell: UITableViewCell { @IBOutlet weak var cosmosView: CosmosView! @IBOutlet weak var productNameLabel: UILabel! override func prepareForReuse() { super.prepareForReuse() // Essential: Reset Cosmos state before cell reuse cosmosView.prepareForReuse() } func configure(productName: String, rating: Double, reviewCount: Int) { productNameLabel.text = productName cosmosView.rating = rating cosmosView.text = "(\(\text{reviewCount}))" cosmosView.settings.updateOnTouch = false // Display only in lists } } // MARK: - Table View Controller class ProductListViewController: UITableViewController { let products = [ ("iPhone Case", 4.5, 1234), ("USB Cable", 3.8, 567), ("Wireless Charger", 4.2, 890) ] override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! ProductTableViewCell let product = products[indexPath.row] cell.configure(productName: product.0, rating: product.1, reviewCount: product.2) return cell } } ``` -------------------------------- ### Assign Settings to Cosmos View in Swift Source: https://github.com/evgenyneu/cosmos/wiki/Cosmos-configuration Assign a pre-configured `CosmosSettings` object to a Cosmos view instance. This allows for centralized management of appearance properties. ```swift var settings = CosmosSettings() // ... configure settings ... cosmos.settings = settings ``` -------------------------------- ### Configure Cosmos for Display-Only Mode (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Set up CosmosView to display ratings without user interaction. This is useful for showing product ratings or review scores. The view will not respond to touch events. ```swift import Cosmos let cosmosView = CosmosView() // MARK: - Read-Only Display cosmosView.settings.updateOnTouch = false cosmosView.rating = 4.2 cosmosView.text = "(847 ratings)" // The view will display the rating but won't respond to touches // Perfect for product listing pages, review displays, etc. ``` -------------------------------- ### SwiftUI Wrapper for Cosmos View Source: https://github.com/evgenyneu/cosmos/wiki/Using-Cosmos-with-SwiftUI This snippet provides a SwiftUI view (`MyCosmosView`) that acts as a bridge to the UIKit-based CosmosView. It uses `UIViewRepresentable` to embed the Cosmos view and allows binding a rating value. It configures basic settings like star size and content hugging priority. ```swift import SwiftUI import Cosmos // A SwiftUI wrapper for Cosmos view struct MyCosmosView: UIViewRepresentable { @Binding var rating: Double func makeUIView(context: Context) -> CosmosView { CosmosView() } func updateUIView(_ uiView: CosmosView, context: Context) { uiView.rating = rating // Autoresize Cosmos view according to it intrinsic size uiView.setContentHuggingPriority(.defaultHigh, for: .vertical) uiView.setContentHuggingPriority(.defaultHigh, for: .horizontal) // Change Cosmos view settings here uiView.settings.starSize = 40 } } struct ContentView: View { @State var rating = 3.0 var body: some View { MyCosmosView(rating: $rating) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } ``` -------------------------------- ### Integrate Cosmos with SwiftUI (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Wrap CosmosView using `UIViewRepresentable` to seamlessly integrate it into SwiftUI views. This allows managing the rating state and handling user interactions within a SwiftUI environment. ```swift import SwiftUI import Cosmos // MARK: - SwiftUI Wrapper struct CosmosSwiftUIView: UIViewRepresentable { @Binding var rating: Double var settings: CosmosSettings = .default func makeUIView(context: Context) -> CosmosView { let cosmosView = CosmosView(settings: settings) // Handle rating changes cosmosView.didFinishTouchingCosmos = { newRating in rating = newRating } return cosmosView } func updateUIView(_ uiView: CosmosView, context: Context) { uiView.rating = rating // Proper sizing for SwiftUI uiView.setContentHuggingPriority(.defaultHigh, for: .vertical) uiView.setContentHuggingPriority(.defaultHigh, for: .horizontal) } } // MARK: - Usage in SwiftUI View struct ProductReviewView: View { @State private var userRating: Double = 0 var body: some View { VStack(spacing: 20) { Text("Rate this product") .font(.headline) CosmosSwiftUIView(rating: $userRating) .frame(height: 50) Text("Your rating: \(userRating, specifier: \"%.1f\") stars") .foregroundColor(.secondary) Button("Submit Review") { print("Submitting rating: \(userRating)") } .disabled(userRating == 0) } .padding() } } ``` -------------------------------- ### Configure Star Color in Swift Source: https://github.com/evgenyneu/cosmos/wiki/Cosmos-configuration Set the color of a filled star in the Cosmos view using Swift. This directly modifies the appearance of the filled stars. ```swift let cosmosView = CosmosView() cosmosView.settings.filledColor = UIColor.orange ``` -------------------------------- ### StarFillMode Enum: Star Fill Display Options (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Illustrates the use of the StarFillMode enum to control how stars are displayed when a rating is not a whole number. It covers .full, .half, and .precise fill modes, showing the visual representation for a rating of 3.7 in each mode. ```swift import Cosmos let cosmosView = CosmosView() cosmosView.rating = 3.7 // MARK: - Full Mode (default) // Shows only fully filled stars. Rating 3.7 displays: ★★★☆☆ cosmosView.settings.fillMode = .full // MARK: - Half Mode // Shows half-filled stars. Rating 3.7 displays: ★★★½☆ cosmosView.settings.fillMode = .half // MARK: - Precise Mode // Fills star according to decimal value. Rating 3.7 displays: ★★★(70%)☆ cosmosView.settings.fillMode = .precise // When configuring in Interface Builder, use raw values: // full = 0, half = 1, precise = 2 ``` -------------------------------- ### Customize Star Images in Cosmos (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Replace default stars with custom images or SF Symbols for filled and empty states. This overrides the vector star points. Ensure images are appropriately sized for best quality. ```swift import Cosmos let cosmosView = CosmosView() // MARK: - Using Custom Star Images cosmosView.settings.filledImage = UIImage(named: "star_filled") cosmosView.settings.emptyImage = UIImage(named: "star_empty") // MARK: - Using SF Symbols (iOS 13+) cosmosView.settings.filledImage = UIImage(systemName: "star.fill") cosmosView.settings.emptyImage = UIImage(systemName: "star") // Note: When images are set, the vector starPoints are ignored // Images should be provided in appropriate sizes for best quality ``` -------------------------------- ### Disable Pan Gestures for Cosmos View in Swift Source: https://github.com/evgenyneu/cosmos/blob/master/README.md Explains how to disable pan gestures on the Cosmos view. This is a specific fix for an issue where iOS 13's modal screen swipe gesture conflicts with Cosmos, preventing it from working correctly. Setting `disablePanGestures` to `true` resolves this. ```swift cosmosView.settings.disablePanGestures = true ``` -------------------------------- ### Configure Cosmos for Modal Screens (iOS 13+) (Swift) Source: https://context7.com/evgenyneu/cosmos/llms.txt Disable pan gestures in CosmosView to prevent conflicts with the swipe-to-dismiss gesture common in modal sheets on iOS 13 and later. This ensures proper interaction when presenting CosmosView in modal contexts. ```swift import Cosmos // MARK: - Fix for Modal Sheets on iOS 13+ let cosmosView = CosmosView() // Disable pan gestures to prevent conflict with modal dismiss gesture cosmosView.settings.disablePanGestures = true // This is essential when presenting CosmosView in: // - Modal sheets with .pageSheet presentation style // - Form sheets // - Any view controller with interactive dismissal ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.