Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to resize the star image on iOS #9

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion EDStarRating.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Pod::Spec.new do |s|
s.version = "1.1"
s.summary = "A configurable star rating control for OSX and iOS, similar to those found in iTunes and the App Store."
s.ios.frameworks = 'CoreGraphics'
s.ios.deployment_target = '4.3'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.6'
s.description = <<-DESC
A configurable star rating control for OSX and iOS, similar to those found in iTunes and the App Store.
Expand Down
4 changes: 4 additions & 0 deletions EDStarRating/EDStarRating.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ typedef UIImage EDImage;
@property (nonatomic,strong) EDImage *backgroundImage;
@property (nonatomic,strong) EDImage *starHighlightedImage;
@property (nonatomic,strong) EDImage *starImage;
@property (nonatomic) NSInteger minRating;
@property (nonatomic) NSInteger maxRating;
@property (nonatomic) float rating;
@property (nonatomic) CGFloat horizontalMargin;
@property (nonatomic) BOOL editable;
@property (nonatomic) EDStarRatingDisplayMode displayMode;
@property (nonatomic) float halfStarThreshold;
#if EDSTAR_IOS
@property (nonatomic) NSInteger starSize;
#endif

@property (nonatomic,weak) id<EDStarRatingProtocol> delegate;
@property (nonatomic,copy) EDStarRatingReturnBlock returnBlock;
Expand Down
121 changes: 107 additions & 14 deletions EDStarRating/EDStarRating.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
// Distributed under MIT license

#import "EDStarRating.h"

#if EDSTAR_IOS
#import <QuartzCore/QuartzCore.h>
#endif
#define ED_DEFAULT_HALFSTAR_THRESHOLD 0.6


Expand All @@ -18,16 +20,20 @@ @interface EDStarRating()


@implementation EDStarRating
@synthesize starImage;
@synthesize starHighlightedImage;
@synthesize starImage = _starImage;
@synthesize starHighlightedImage = _starHighlightedImage;
@synthesize rating=_rating;
@synthesize minRating;
@synthesize maxRating;
@synthesize backgroundImage;
@synthesize editable;
@synthesize delegate=_delegate;
@synthesize horizontalMargin;
@synthesize halfStarThreshold;
@synthesize displayMode;
#if EDSTAR_IOS
@synthesize starSize=_starSize;
#endif
#if EDSTAR_MACOSX
@synthesize backgroundColor=_backgroundColor;
#endif
Expand All @@ -39,9 +45,10 @@ @implementation EDStarRating

-(void)setDefaultProperties
{
maxRating=5.0;
_rating=0.0;
horizontalMargin=10.0;
minRating = 1.0;
maxRating = 5.0;
_rating = 0.0;
horizontalMargin = 10.0;
displayMode = EDStarRatingDisplayFull;
halfStarThreshold=ED_DEFAULT_HALFSTAR_THRESHOLD;
[self setBackgroundColor:[EDColor clearColor]];
Expand Down Expand Up @@ -110,11 +117,23 @@ -(void)setDisplayMode:(EDStarRatingDisplayMode)dispMode
[self setNeedsDisplay];
}

#if EDSTAR_IOS
-(void)setStarSize:(NSInteger)starSize
{
_starSize = starSize;
if(_starSize != 0 && self.starImage != nil && self.starHighlightedImage != nil){
self.starImage = [self resizeImageToSize:CGSizeMake(_starSize, _starSize) withImage:self.starImage];
self.starHighlightedImage = [self resizeImageToSize:CGSizeMake(_starSize, _starSize) withImage:self.starHighlightedImage];
}
[self setNeedsDisplay];
}
#endif

#pragma mark -
#pragma mark Drawing
-(CGPoint)pointOfStarAtPosition:(NSInteger)position highlighted:(BOOL)hightlighted
{
CGSize size = hightlighted?starHighlightedImage.size:starImage.size;
CGSize size = hightlighted?_starHighlightedImage.size:_starImage.size;

NSInteger starsSpace = self.bounds.size.width - 2*horizontalMargin;

Expand Down Expand Up @@ -220,7 +239,7 @@ -(void)drawRect:(CGRect)rect
}

// Draw rating Images
CGSize starSize = starHighlightedImage.size;
CGSize starSize = _starHighlightedImage.size;
for( NSInteger i=0 ; i<maxRating; i++ )
{
[self drawImage:self.tintedStarImage atPosition:i];
Expand Down Expand Up @@ -284,6 +303,11 @@ -(float) starsForPoint:(CGPoint)point
stars+=increment;
}
}

if(stars < minRating){
stars = minRating;
}

return stars;
}

Expand All @@ -296,7 +320,6 @@ -(void)mouseDown:(NSEvent *)theEvent
if ([theEvent type] == NSLeftMouseDown) {

NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];

self.rating = [self starsForPoint:pointInView];
[self setNeedsDisplay];
}
Expand All @@ -310,7 +333,7 @@ -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
self.rating =[self starsForPoint:touchLocation];
self.rating = [self starsForPoint:touchLocation];
[self setNeedsDisplay];
}
#endif
Expand Down Expand Up @@ -360,22 +383,29 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
#pragma mark - Tint color Support
-(void)setStarImage:(EDImage *)image
{
if( starImage == image)
if( _starImage == image)
return;

starImage = image;
_starImage = image;
if(_starSize != 0){
_starImage = [self resizeImageToSize:CGSizeMake(_starSize, _starSize) withImage:_starImage];
}
self.tintedStarImage = [self tintedImage:image];
}

-(void)setStarHighlightedImage:(EDImage *)image
{
if( starHighlightedImage == image )
if( _starHighlightedImage == image )
return;

starHighlightedImage = image;
_starHighlightedImage = image;
if(_starSize != 0){
_starHighlightedImage = [self resizeImageToSize:CGSizeMake(_starSize, _starSize) withImage:_starHighlightedImage];
}
self.tintedStarHighlightedImage = [self tintedImage:image];

}

-(EDImage*)tintedImage:(EDImage*)img
{
EDImage *tintedImage = img;
Expand Down Expand Up @@ -412,4 +442,67 @@ -(void)tintColorDidChange
}
#endif

#pragma mark -
#pragma mark Helper methods

#if EDSTAR_IOS
- (UIImage *)resizeImageToSize:(CGSize)targetSize withImage:(UIImage*)image
{
UIImage *sourceImage = image;
UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;

CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;

CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;

if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;

scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;

// make image center aligned
if (widthFactor < heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor > heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}

UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

if(newImage == nil)
NSLog(@"Could not scale image");

return newImage ;
}
#endif

@end
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
EDStarRating ![Version Badge](https://go-shields.herokuapp.com/license-BSD-blue.png)
===============

A configurable star rating control for OSX and iOS, similar to those found in iTunes and the App Store.
A configurable star rating control for OSX and iOS, similar to those found in iTunes and the App Store. Fork of https://github.com/erndev/EDStarRating including support for resizing large star images on iOS. Additionally I have rewritten the iOS sample project with the additional presentation of the resizing code.

[![Badge w/ Version](https://cocoapod-badges.herokuapp.com/v/EDStarRating/badge.png)](https://cocoadocs.org/docsets/EDStarRating)
[![Badge w/ Platform](https://cocoapod-badges.herokuapp.com/p/EDStarRating/badge.svg)](https://cocoadocs.org/docsets/EDStarRating)

###Installation

The easiest way to install EDStarRating is via [CocoaPods](http://cocoapods.org). Add this line to your Podfile:
The easiest way to install this fork of EDStarRating is via [CocoaPods](http://cocoapods.org). Add this line to your Podfile:

```ruby
pod 'EDStarRating'
pod 'EDStarRating', :git => 'https://github.com/martinpfannemueller/EDStarRating.git'
```

and run `pod install`.
and run `pod install`.

You can also install it manually by copying these two files to your project: EDStarRating.h and EDStarRating.m

Expand All @@ -24,10 +24,11 @@ You can also install it manually by copying these two files to your project: EDS

Example:

```
```objective-c

starRating.starImage = [NSImage imageNamed:@"star.png"];
starRating.starHighlightedImage = [NSImage imageNamed:@"starhighlighted.png"];
starRating.minRating = 1.0; // Prevents ratings below a minimum
starRating.maxRating = 5.0;
starRating.delegate = self;
starRating.horizontalMargin = 12;
Expand All @@ -37,6 +38,9 @@ Example:

starRating.rating= 2.5;

// Resizing of large star image by setting starSize property
starRating.starSize = 20;

```
### tintColor support in iOS 7
If you pass to the control a template UIImage (created using rendering mode (UIImageRenderingModeAlwaysTemplate), EDStarRating will tint that image using the control's tintColor property.
Expand All @@ -45,12 +49,12 @@ If you pass to the control a template UIImage (created using rendering mode (UII
### Compatiblity
This control should work in ARC and not ARC projects. On OS X 10.6+ and iOS 4.x+.
But it's only tested it with ARC enabled in 10.8+ and iOS 6.0+. Let me now if it works fine for you in other environments.


### Screenshots

![EDStarRating](https://github.com/erndev/EDStarRating/raw/master/edstarrating.png)
![EDStarRating](https://github.com/erndev/EDStarRating/raw/master/edstarrating-ios.png)
![EDStarRating](https://raw.githubusercontent.com/martinpfannemueller/EDStarRating/master/edstarrating-ios.png)

### License
BSD License.
Expand Down Expand Up @@ -80,5 +84,5 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

### Star icons
The star icons and backgrounds used in the sample have been created by [Dan Deming-Henes
The star icons and backgrounds taken from the original project used in the sample have been created by [Dan Deming-Henes
](http://strandeddesign.com).
Loading