Giter Club home page Giter Club logo

cosmos's Introduction

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" ~> 25.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', '~> 25.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.

cosmos's People

Contributors

benpackard avatar chlumik avatar craiggrummitt avatar danshevluk avatar dkk avatar evgenyneu avatar gcharita avatar hvsw avatar jsahoo avatar kimjitaee avatar maxzheleznyy avatar mickmaccallum avatar mikezander avatar quanganhdo avatar staticdreams avatar thelvis4 avatar wagnersouz4 avatar wlxo0401 avatar yuravake avatar zahedigol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cosmos's Issues

Changelog File

Please add a CHANGELOG.md file to make it easier to see what's changed with each release.

I am receiving a warning that M_PI is deprecated in Xcode 8.3.3

/Users/carlosdelamora/Desktop/my apps/MonEx/Pods/Cosmos/Cosmos/CosmosLayers.swift:93:56: 'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.

In order to change it to Double.pi as the message suggest I need to unlock cosmos code, I do not want to do this. Is there another way around it, like an update or something?

On click of an already rated star how can I uncheck it

I have followed the readme and implemented the Cosmos view.
I have set the fill mode to half. However only from the second star I am able to rate half a star. The first star fills up completely.
Also on click of a star that is already filled I want to uncheck it i.e I want the rating to be 0 if the rating was 1 . How can I solve this?

Swift 1.2 not compatible

I am working on a project using Swift 1.2 and am unable to use Cosmos is there a 1.2 compatible version still available?

Swift 2.3 compatible version in Cocoapods

We are using this library in other library and we need to be compatible with swift 2.3, Can you please upload the latest changes of the swift2 branch to cocoapods? It would be a great help

Thank you!

Kind regards

Custom image?

Your library is a great example of modern iOS control, I really appreciate your effort!

Is there an easy way to use a custom images instead of stars?

is there any didTouchFinish?!

hi.
thank for your great work.
how i can know if touch is finished?
i want to send selected rate to server after user set the rate.
i used didTouchCosmos,but it is not good way to send rated value to server!

Set rating on tap

I may be doing something wrong, but currently I can only set the rating by touching and sliding from left to right. I cannot simply tap a star to set the rating. If this is possible, how is it implemented?

Thanks,
Jerry

Changing size of individual star?

Hi, great work with the library!

Is there a way to change the size of one star and keep the others unchanged? I want the size of one star to increasingly get bigger the longer a user holds it.

How To save Ratings in variables?

Hi,

Could you provide a short example on how to use didFinishTouchingCosmos and save values in variables?
It is not very clear to me in the read me file, sorry.

thanks,

Error when load it to TableView

I drag a view and set its class to CosmosView, but when I run the project, it crashes:

Failed to set (rateLineWidth) user defined inspected property on (Cosmos.CosmosView): [<Cosmos.CosmosView 0x7ff150d53e70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key rateLineWidth.

Align rating to Right inside the cosmos uiview

I want to align cosmos view to right without applying fixed size width constraint. For example I am having cosmos view of width 80 but it's actual content (i.e. 5 stars) are of width 60 which I want them to align right instead of left.

Please help.

IB Designable error

Main.storyboard: warning: IB Designables: Using class UIView for object with custom class because the class CosmosView does not exist

--

If I am missing a setting or checking off something, please let me know. I am a new user to this control.

-Kevin.

How to trigger an action after user selecting certain number of stars

How to trigger an action after user selecting certain number of stars, e.g. if user selected all five stars that are shown, the app wants to show an alert.

The author provided two functions:

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

which seem to be for the purpose.

Can you please tell me where I can use the above pieces of code? You do not seem to provide any deletage like UITableView.

I am a beginner iOS programmer so I really hope you can help.

Cosmos on UITableViewCell

How can we use Cosmos Star on UITableViewCell , since my requirement is to get multiple rating stars to rate on specific cell index and send to server

Please help add view in code

Could you please help me how to write the code to add Cosmos without storyboard? I need a row with a variable amount of cosmos UIViews depending on the size of an array.

I did not understand how you meant this:
"Alternatively, one can instantiate CosmosView class and add it to the view manually without using Storyboard."

Thank you very much in advance!!

Unable to select more than 5 stars with default size settings

I set 6 total stars and a star size of 30.0, in this way I cannot select more than 4 stars.
Setting 6 total star and the default star size, I cannot select more than 5 stars.
Seems like the touchable area remains always at the default size, not updating once the drawn area changed.

Code-compliant issues

Recently i've downloaded the library and embedded it into my project using Cocoapods ~ 1.1.1. Everything worked fine until when i tried to add Cosmos rating view into table cell and try to manipulate it with different rating values on the controller side and end up getting this error during run-time:

*** Terminating app due to uncaught exception 'NSUnknownKeyExeption', reason: setValue:forUnderfinedKey:]: this class is not key value coding-compliant for the key RatingView.***

Is there anything that i might have missed, i tried to follow everything as explained at the intro yet this error still exists.

IDE: Xcode ~ 8.2.1

not exist cosmos

hello @evgenyneu evgeny
when I Set the view's class to CosmosView in the Identity Inspector .
warning: IB Designables: Using class UIView for object with custom class because the class CosmosView does not exist
Because you know it ?

Text positioning

Is there a way to positioning the text in other position then on the right?
It was nice to be possible to position the text on either of the sides, top, bottom, left and right.

Thanks.

didTouchCosmos called Twice

Its working in Emulator, but in device if not selected precisely inside star, didTouch is getting called twice and sets the max rating possible.

tableviewcell implementation

Hi I am using custom tableview cell and trying to implemet your control into my project.
In my tableviewcell class I am creating Outlet @IBOutlet weak var ratingStar: TegStarRatingView!

Than in my cellforatindexpath

doing this:

cell.ratingStar.settings.starColorFilled = UIColor.blackColor()
cell.ratingStar.settings.starFillMode = TegStarFillMode.Full
cell.ratingStar.show(rating: 3.0)

but getting always this error
fatal error: unexpectedly found nil while unwrapping an Optional value

//debug console
po cell.ratingStart
nil

What I am doing wrong?

Using Cosmos in tvOS app with CocoaPods shows error Library not loaded: @rpath/libswiftCore.dylib

@staticdreams, thanks for adding tvOS support. I was testing this change by creating a new tvOS app and adding Cosmos via CocoPods. Then I created a view in storyboard and assigned its class to CosmosView. After that I got an error:

... Base.lproj/Main.storyboard: error: IB Designables: Failed to render and update auto layout status for ViewController (BYZ-38-t0r): dlopen(Cosmos.framework, 1): Library not loaded: @rpath/libswiftCore.dylib
Referenced from: Cosmos.framework
Reason: image not found

I tried using Cosmos with file and Carthage methods in tvOS app - both worked. It is the CocoaPods method that does not work.

Right To Left Support

Hi,
first of all i'd like to thank you for this framework but i have a question . i there any plane for this framework to support right to left languages ?

xCode8 , Swift 2.3 and Storyboard

Hello,

First of all , congratulations for your library. It's amazing!
I'm working on update my project to xCode 8, but I got an issue with Cosmos framework.
This is the message I get from xCode:

error: IB Designables: Failed to update auto layout status: dlopen(Cosmos.framework, 1): Symbol not found: __TWPSis16ForwardIndexTypes
  Referenced from: Cosmos.framework
  Expected in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays/../../../../../../Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator/libswiftCore.dylib
 in Cosmos.framework

Do you have any idea of how to solve this problem?

Thanks in advance,
Renan.

Xcode 8.1 - Swift 3.0.1 support

Hi,

There is a support for these versions (Xcode 8.1 - Swift 3.0.1)?

I have this error:

“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

Thanks!

Does not compile from within an Extension

When compiling from within an extension (e.g. a Notification Content Extension), UIApplication.shared is not available and the compiler throws an error in RightToLeft.swift

...Cosmos/Helpers/RightToLeft.swift:14:28: 'shared' is unavailable: Use view controller based solutions where appropriate instead.

Selecting by swipe

Thank you for this nice and simple UI item!

What setting should I use in order to enable selecting star by swipe. Right now I can use selection of only by tapping one of stars

Readme wrong re: Swift 2.3 branch?

Hi there,

Thanks so much for this awesome library!

My company is still using swift 2.3, and adding github "marketplacer/Cosmos" ~> 1.2 to the Cartfile did not work. It did work when I added github "marketplacer/Cosmos" "swift-2.3", however, so I don't think the branch itself is the issue. Is there possibly a typo in the readme regarding the version number?

Thanks in advance!

Cosmos not "touching" in tableviewcell

Hi! i set up a tableviewcell with a few components including a cosmos view. At first i was able to get feedback using the didFinishTouchingCosmos function on my cell after i called the dequeueReusableCellWithIdentifier. But for some reason now, the cosmos interaction has seemed to have been turned off. Any idea why this is happening?

Can't change value of cosmos

Similar to #31, I have a CosmosView inside a UITableViewCell. Normally it works fine.

However, when I add a UITapGestureRecognizer to my UITableView, I can no longer change the value of CosmosView by tapping.

I have to hold touch on CosmosView to actually work after adding the UITapGestureRecognizer.

I am using UITapGestureRecognizer for dismiss keyboard functionality. And this is how I add UITapGestureRecognizer.

        let tapQuit = UITapGestureRecognizer(target: self, action: #selector(FeedTableViewController.dismissKeyboard(_:)))
        tableView.addGestureRecognizer(tapQuit)

I have also enabled cosmosview.settings.updateOnTouch = true

So my question is, is there any way to make it work with UITapGestureRecognizer. Or should I just get rid of UITapGestureRecognizer, and try to solve dismiss keyboard with some other way?

Thanks!

didTouchCosmos closure can capture self

I'd be safer to change this

cosmosView.didTouchCosmos = { rating in }

to this

cosmosView.didTouchCosmos = { [unowned self] rating in }

in case if we'd use this code within view controller. Otherwise didTouchCosmos closure captures a reference to self and retain cycle is created.

Can't get Tap Gesture UIState Began

I needed to know when the cosmo stars are being pressed to caused other things in the view to happen. It seems when you add a UITapGestureRecognizer on the CosmoView you only get Ended and don't get began. Amazing job otherwise.

`let gestureViewCosmoTapped: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapGestureViewCosmo))

func tapGestureViewCosmo(sender: UITapGestureRecognizer){
print("Tap : " + String(sender.state.rawValue))
if (sender.state == UIGestureRecognizerState.Ended){
print("ended")
}else if (sender.state == UIGestureRecognizerState.Changed){
print("changed")
}else if (sender.state == UIGestureRecognizerState.Began){
print("began")
}else if (sender.state == UIGestureRecognizerState.Cancelled){
print("cancelled")
}else{
}
}`

Having trouble getting stars to show

I am putting a View inside a TableViewCell via storyboard. I've set the width of the view to 60, height to 20.
I set the view to class ComosView, and via the attribute inspector, I set
Rating: 5, text 0, total Stars 5, star size 20.
Filled colors, empty colors...etc.

However, when i run it in the simulator, (i expect to see 5 stars filled). But the view is empty.
I have no idea why. What might I be doing wrong?

Secondary question, I've also tried to set rating programatically when configuring the tableviewcell, however, I get a exc_bad_access.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.