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

Added ability to set min rating #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EDStarRating/EDStarRating.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ 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;
Expand Down
67 changes: 27 additions & 40 deletions EDStarRating/EDStarRating.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,19 @@ @interface EDStarRating()


@implementation EDStarRating
@synthesize starImage;
@synthesize starHighlightedImage;
@synthesize rating=_rating;
@synthesize maxRating;
@synthesize backgroundImage;
@synthesize editable;
@synthesize delegate=_delegate;
@synthesize horizontalMargin;
@synthesize halfStarThreshold;
@synthesize displayMode;
#if EDSTAR_MACOSX
@synthesize backgroundColor=_backgroundColor;
#endif
@synthesize returnBlock=_returnBlock;

#pragma mark -
#pragma mark Init & dealloc


-(void)setDefaultProperties
{
maxRating=5.0;
_rating=0.0;
horizontalMargin=10.0;
displayMode = EDStarRatingDisplayFull;
halfStarThreshold=ED_DEFAULT_HALFSTAR_THRESHOLD;
_minRating = 0.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 @@ -106,23 +93,23 @@ -(void)setRating:(float)ratingParam

-(void)setDisplayMode:(EDStarRatingDisplayMode)dispMode
{
displayMode = dispMode;
_displayMode = dispMode;
[self setNeedsDisplay];
}

#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;
NSInteger starsSpace = self.bounds.size.width - 2*_horizontalMargin;

NSInteger interSpace = 0;
interSpace = maxRating-1>0?(starsSpace - (maxRating)*size.width)/(maxRating-1):0;
interSpace = _maxRating-1>0?(starsSpace - (_maxRating)*size.width)/(_maxRating-1):0;
if( interSpace <0 )
interSpace=0;
CGFloat x = horizontalMargin + size.width*position;
CGFloat x = _horizontalMargin + size.width*position;
if( position >0 )
x+=interSpace*position;
CGFloat y = (self.bounds.size.height - size.height)/2.0;
Expand All @@ -131,13 +118,13 @@ -(CGPoint)pointOfStarAtPosition:(NSInteger)position highlighted:(BOOL)hightlight

-(void)drawBackgroundImage
{
if( backgroundImage )
if( _backgroundImage )
{
#if EDSTAR_MACOSX
[backgroundImage drawInRect:self.bounds fromRect:NSMakeRect(0.0, 0.0, backgroundImage.size.width, backgroundImage.size.height) operation:NSCompositeSourceOver fraction:1.0];

#else
[backgroundImage drawInRect:self.bounds];
[_backgroundImage drawInRect:self.bounds];

#endif
}
Expand Down Expand Up @@ -214,14 +201,14 @@ -(void)drawRect:(CGRect)rect
CGContextFillRect(ctx, bounds);

// Draw background Image
if( backgroundImage )
if( _backgroundImage )
{
[self drawBackgroundImage];
}

// Draw rating Images
CGSize starSize = starHighlightedImage.size;
for( NSInteger i=0 ; i<maxRating; i++ )
CGSize starSize = _starHighlightedImage.size;
for( NSInteger i=0 ; i<_maxRating; i++ )
{
[self drawImage:self.tintedStarImage atPosition:i];
if( i < _rating ) // Highlight
Expand All @@ -236,11 +223,11 @@ -(void)drawRect:(CGRect)rect
CGRect rectClip;
rectClip.origin = starPoint;
rectClip.size = starSize;
if( displayMode == EDStarRatingDisplayHalf && difference < halfStarThreshold ) // Draw half star image
if( _displayMode == EDStarRatingDisplayHalf && difference < _halfStarThreshold ) // Draw half star image
{
rectClip.size.width/=2.0;
}
else if( displayMode == EDStarRatingDisplayAccurate )
else if( _displayMode == EDStarRatingDisplayAccurate )
{
rectClip.size.width*=difference;
}
Expand All @@ -265,8 +252,8 @@ -(void)drawRect:(CGRect)rect
#pragma mark Mouse/Touch Interaction
-(float) starsForPoint:(CGPoint)point
{
float stars=0;
for( NSInteger i=0; i<maxRating; i++ )
float stars = _minRating;
for( NSInteger i=stars; i<_maxRating; i++ )
{
CGPoint p =[self pointOfStarAtPosition:i highlighted:NO];
if( point.x > p.x )
Expand Down Expand Up @@ -305,7 +292,7 @@ -(void)mouseDown:(NSEvent *)theEvent
#else
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if( !editable )
if( !_editable )
return;

UITouch *touch = [touches anyObject];
Expand All @@ -328,7 +315,7 @@ -(void)mouseDragged:(NSEvent *)theEvent
#else
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if( !editable )
if( !_editable )
return;

UITouch *touch = [touches anyObject];
Expand All @@ -345,7 +332,7 @@ - (void)mouseUp:(NSEvent *)theEvent
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
#endif
{
if( !editable )
if( !_editable )
return;

if( self.delegate && [self.delegate respondsToSelector:@selector(starsSelectionChanged:rating:)] )
Expand All @@ -360,19 +347,19 @@ - (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;
self.tintedStarImage = [self tintedImage:image];
}

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

starHighlightedImage = image;
_starHighlightedImage = image;
self.tintedStarHighlightedImage = [self tintedImage:image];

}
Expand Down
10 changes: 9 additions & 1 deletion SampleiOS/SampleiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
94B6314D15E4E53F00C7F229 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0500;
LastUpgradeCheck = 0610;
};
buildConfigurationList = 94B6315015E4E53F00C7F229 /* Build configuration list for PBXProject "SampleiOS" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -244,9 +244,11 @@
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -278,9 +280,11 @@
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand All @@ -299,9 +303,11 @@
94B6317515E4E53F00C7F229 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "SampleiOS/SampleiOS-Prefix.pch";
INFOPLIST_FILE = "SampleiOS/SampleiOS-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -310,9 +316,11 @@
94B6317615E4E53F00C7F229 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "SampleiOS/SampleiOS-Prefix.pch";
INFOPLIST_FILE = "SampleiOS/SampleiOS-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down
3 changes: 2 additions & 1 deletion SampleiOS/SampleiOS/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ - (void)viewDidLoad
_starRatingImage.backgroundImage=[UIImage imageNamed:@"starsbackground iOS.png"];
_starRatingImage.starImage = [UIImage imageNamed:@"star.png"];
_starRatingImage.starHighlightedImage = [UIImage imageNamed:@"starhighlighted.png"];
_starRatingImage.minRating = 2.0;
_starRatingImage.maxRating = 5.0;
_starRatingImage.delegate = self;
_starRatingImage.horizontalMargin = 12;
_starRatingImage.editable=YES;
_starRatingImage.rating= 2.5;
_starRatingImage.rating= 0;
_starRatingImage.displayMode=EDStarRatingDisplayAccurate;
[self starsSelectionChanged:_starRatingImage rating:2.5];
// This one will use the returnBlock instead of the delegate
Expand Down