• Stars
    star
    2,119
  • Rank 21,764 (Top 0.5 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A star rating control for iOS/tvOS written in Swift

Cosmos, a star rating control for iOS and tvOS

Carthage compatible CocoaPods Version Swift Package Manager compatible License Platform

Cosmos, star rating control for iOS / Swift

This is a UI control for iOS and tvOS written in Swift. It shows a star rating and takes rating input from the user. Cosmos is a subclass of a UIView that will allow your users to post those inescapable 1-star reviews!

  • Shows star rating with an optional text label.
  • Can be used as a rating input control (iOS only).
  • Cosmos view can be customized in the Storyboard without writing code.
  • Includes different star filling modes: full, half-filled and precise.
  • Cosmos is accessible and works with voice-over.
  • Supports right-to-left languages.

Binary star system of Sirius A and Sirius B (artist's impression)

Picture of binary star system of Sirius A and Sirius B by NASA, ESA and G. Bacon (STScI). Source: spacetelescope.org.

Video tutorial

Thanks to Alex Nagy from rebeloper.com for creating this amazing video tutorial that shows how to use and customize Cosmos from code.

Cosmos rating video tutorial for Swift 4.2 (Xcode10)

Setup

There are various ways you can add Cosmos to your Xcode project.

Add source (iOS 8+)

Simply add CosmosDistrib.swift file into your Xcode project.

Setup with Carthage (iOS 8+)

Alternatively, add github "evgenyneu/Cosmos" ~> 23.0 to your Cartfile and run carthage update.

Setup with CocoaPods (iOS 8+)

If you are using CocoaPods add this text to your Podfile and run pod install.

use_frameworks!
target 'Your target name'
pod 'Cosmos', '~> 23.0'

Setup with Swift Package Manager

Legacy Swift versions

Setup a previous version of the library if you use an older version of Swift.

Usage

  1. Drag View object from the Object Library into your storyboard.

Add view control in attributes inspector

  1. Set the view's class to CosmosView in the Identity Inspector. Set its module property to Cosmos, unless you used the file setup method.

Add Cosmos rating view to the storyboard

tvOS note: read the collowing setup instructions for tvOS if you see build errors at this stage.

  1. Customize the Cosmos view appearance in the Attributes Inspector. If storyboard does not show the stars click Refresh All Views from the Editor menu.

Customize cosmos appearance in the attributes inspector in Xcode.

Positioning the Cosmos view

One can position the Cosmos view using Auto Layout constaints. The width and height of the view is determined automatically based on the size of its content - stars and text. Therefore, there is no need to set width/height constaints on the Cosmos view.

Using Cosmos in code

Add import Cosmos to your source code, unless you used the file setup method.

You can style and control Cosmos view from your code by creating an outlet in your view controller. Alternatively, one can instantiate CosmosView class and add it to the view manually without using Storyboard.

// 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 }

Customization

One can customize Cosmos from code by changing its settings. See the Cosmos configuration manual for the complete list of configuration options.

// 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

Supplying images for the stars

By default, Cosmos draws the stars from an array of points. Alternatively, one can supply custom images for the stars, both in the Storyboard and from code.

Using star images from the Storyboard

Supplying an image for a star in Xcode.

Using star images from code

// Set image for the filled star
cosmosView.settings.filledImage = UIImage(named: "GoldStarFilled")

// Set image for the empty star
cosmosView.settings.emptyImage = UIImage(named: "GoldStarEmpty")

Note: you need to have the images for the filled and empty star in your project for this code to work.

Download star image files

Images for the golden star used in the demo app are available in here. Contributions for other star images are very welcome: add vector images to /graphics/Stars/ directory and submit a pull request.

Using Cosmos in a scroll/table view

Here is how to use Cosmos in a scroll view or a table view.

Using Cosmos with SwiftUI

Here is how to show a Cosmos view with SwiftUI.

Using Cosmos in a modal screen

iOS 13 introduced swiping gesture for closing modal screens, which prevents Cosmos from working properly. The following setting fixes this problem:

cosmosView.settings.disablePanGestures = true

Using Cosmos settings from Objective-C

This manual describes how to set/read Cosmos settings in Objective-C apps.

Demo app

This project includes a demo iOS app.

Five star rating control for iOS written in Swift

Using cosmos in a table view

Using cosmos in a table view

Alternative solutions

Here are some other star rating controls for iOS:

Thanks 👍

We would like to thank the following people for their valuable contributions.

  • jsahoo for adding ability to customize the Cosmos view from the interface builder with Carthage setup method.
  • 0x7fffffff for changing public access-level modifiers to open.
  • ali-zahedi for updating to the latest version of Swift 3.0.
  • augmentedworks for adding borders to filled stars.
  • craiggrummitt for Xcode 8 beta 4 support.
  • JimiSmith for Xcode 8 beta 6 support.
  • nickhart for adding compatibility with Xcode 6.
  • staticdreams for bringing tvOS support.
  • wagnersouz4 for Swift 3.1 update.
  • paoloq for reporting the CosmoView frame size issue when the view is used without Auto Layout.
  • danshevluk for adding ability to reuse settings in multiple cosmos views.
  • xrayman for reporting a table view reusability bug and improving the table view screen of the demo app.
  • chlumik for updating to Swift 4.2.
  • rebeloper for creating a video tutorial.
  • yuravake for adding passTouchesToSuperview setting.
  • gcharita for adding Swift Package Manager support.
  • benpackard for fixing Cosmos when used in a modal screen on iOS 13.
  • dkk for the dark mode update.

License

Cosmos is released under the MIT License.

🌌⭐️🌕🚀🌠

We are a way for the cosmos to know itself.

Carl Sagan, from 1980 "Cosmos: A Personal Voyage" TV series.

More Repositories

1

keychain-swift

Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.
Swift
2,577
star
2

Dodo

A message bar for iOS written in Swift.
Swift
875
star
3

SigmaSwiftStatistics

A collection of functions for statistical calculation written in Swift.
Swift
682
star
4

js-evaluator-for-android

A library for running JavaScript in Android apps.
JavaScript
476
star
5

swift-badge

A badge view for iOS/tvOS written in Swift
Swift
390
star
6

moa

An image download extension of the image view written in Swift for iOS, tvOS and macOS.
Swift
333
star
7

Auk

An image slideshow for iOS written in Swift.
Swift
277
star
8

center-vfl

Centering a view in a super view with Visual Format Language using Auto Layout in iOS/Swift
Swift
227
star
9

ios-imagescroll-swift

iOS demo of using image view inside a scroll view with auto layout in Swift
Swift
128
star
10

UnderKeyboard

An iOS libary for moving content from under the keyboard.
Swift
128
star
11

SpringAnimationCALayer

A helper function for animating CALayer with spring effect in Swift
Swift
117
star
12

Cephalopod

A sound fader for AVAudioPlayer written in Swift for iOS, tvOS and macOS.
Swift
112
star
13

bubble-button-animation-ios-swift

Demo iOS app showing button animation
Swift
66
star
14

bounce-button-animation-android

A demo Android app that shows how to animate a button with spring/bounce effect.
Java
61
star
15

ios-imagescroll

iOS: example of using image view inside a scroll view with auto layout, written in Obective-C.
Objective-C
59
star
16

angular-markdown-preview

AngularJS module for markdown textarea with live HTML preview.
JavaScript
35
star
17

ios-javascriptcore-demo

iOS demo: running javascript function from ObjectiveC using JavaScriptCore framework
Objective-C
32
star
18

Glyptodon

A 'no content' message overlay for iOS written in Swift.
Swift
31
star
19

Scrollable

An iOS control that makes content scroll vertically.
Swift
20
star
20

aes-crypto-web

Web app for encrypting text messages http://aescrypto.com
JavaScript
20
star
21

JsonSwiftson

A JSON parser with concise API written in Swift.
Swift
14
star
22

aes-crypto-android

Android app for encrypting text messages http://aescrypto.com
JavaScript
12
star
23

two_galaxies

A web simulation of two interacting galaxies
JavaScript
10
star
24

walk-to-circle-ios

An iOS app for those who like walking
Swift
9
star
25

trigonometric_identities_latex

Trigonometric identities in LaTeX format
TeX
8
star
26

greeter-android

Sample code for "Testing activity in Android Studio" tutorial.
Java
6
star
27

aes-text-encryption-ios

iOS app for encrypting text messages http://aescrypto.com
JavaScript
5
star
28

sharing-keychain-demo

A demo iOS app showing how to access a shared keychain item
Swift
4
star
29

ios-core-location-battery-meter

iOS demo app for measuring power consumption of location services
Swift
4
star
30

core-location-tester-ios

Demo iOS app for testing Core Location accuracy
Swift
3
star
31

SneakPeekScroll

An iOS demo app showing a scroll view with images and lets you see a portion of the next and previous images
Swift
3
star
32

evgenii.com

My personal web site at http://evgenii.com
JavaScript
3
star
33

FortranFromPython

An example of running a Fortran code from Python
Python
3
star
34

siba

Simple backup and restore utility
Ruby
2
star
35

tarpan

Python functions for analysing cmdspanpy output
Python
2
star
36

image_compressor_c

An image compressor program written in C that uses singular value expansion
C
2
star
37

docker-rails-fig-sample

A sample Rails/Postgres app that shows how to use Docker and Fig
Ruby
2
star
38

oct_viewer

Demo of the OCT viewer
JavaScript
1
star
39

siba-destination-ftp

FTP destination for SIBA backup and restore utility.
Ruby
1
star
40

watch-kit-app-demo

Demo WatchKit app from the video tutorial.
Swift
1
star
41

walk-to-circle-android

An Android app for those who like walking
Java
1
star
42

distance_from_parallax

A Python program that plots distances measured from simulated parallaxes.
Python
1
star
43

covid19

A naive Stan model of confirmed COVID-19 cases that uses logistic function.
Python
1
star
44

siba-source-mysql

Backup and restore MySQL database. This is an extension to SIBA utility.
Ruby
1
star
45

ai_teaches_me_ml

This is my logbook from my interactive lessons where ChatGPT teaches me ML
Jupyter Notebook
1
star
46

siba-destination-aws-s3

Backup to Amazon S3 cloud storage. This is an extension to SIBA backup and restore utility.
Ruby
1
star
47

SixBouncingButtons

An example of creating six buttons with bounce effect in Android
Java
1
star
48

siba-source-mongo-db

MongoDb backup and restore plugin for SIBA
Ruby
1
star
49

ASP3162

Codes for ASP3162 labs
Fortran
1
star
50

WatchKitParentAppBenchmark

A demo app showing how to communicate between watchOS and iOS using the sendMessage method of watch connectivity framework.
Swift
1
star