Skip to content

Commit

Permalink
Added star fill/empty colour
Browse files Browse the repository at this point in the history
star border colour and border width
  • Loading branch information
irfangul92 authored and hsousa committed Mar 14, 2017
1 parent 4929638 commit ff8935e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
6 changes: 6 additions & 0 deletions HCSStarRatingView/HCSStarRatingView.h
Expand Up @@ -39,6 +39,12 @@ IB_DESIGNABLE
// Optional: if `nil` method will return `NO`.
@property (nonatomic, copy) HCSStarRatingViewShouldBeginGestureRecognizerBlock shouldBeginGestureRecognizerBlock;

@property (nonatomic, strong) IBInspectable UIColor *starBorderColor;
@property (nonatomic) IBInspectable CGFloat starBorderWidth;

@property (nonatomic, strong) IBInspectable UIColor *emptyStarColor;
@property (nonatomic, strong) IBInspectable UIColor *filledStarColor;

@property (nonatomic, strong) IBInspectable UIImage *emptyStarImage;
@property (nonatomic, strong) IBInspectable UIImage *halfStarImage;
@property (nonatomic, strong) IBInspectable UIImage *filledStarImage;
Expand Down
49 changes: 46 additions & 3 deletions HCSStarRatingView/HCSStarRatingView.m
Expand Up @@ -62,6 +62,12 @@ - (void)_customInit {
_value = 0;
_spacing = 5.f;
_continuous = YES;
_starBorderWidth = 1.0f;
_starBorderColor = self.tintColor;
_emptyStarColor = [UIColor clearColor];
_filledStarColor = self.tintColor;


[self _updateAppearanceForState:self.enabled];
}

Expand Down Expand Up @@ -161,6 +167,38 @@ - (void)setFilledStarImage:(UIImage *)filledStarImage {
}
}


- (void)setFilledStarColor:(UIColor *)filledStarColor {
if(_filledStarColor != filledStarColor) {
_filledStarColor = filledStarColor;
[self setNeedsDisplay];
}
}

- (void)setEmptyStarColor:(UIColor *)emptyStarColor {
if(_emptyStarColor != emptyStarColor) {
_emptyStarColor = emptyStarColor;
[self setNeedsDisplay];
}
}

- (void)setStarBorderColor:(UIColor *)startBorderColor {
if(_starBorderColor != startBorderColor) {
_starBorderColor = startBorderColor;
[self setNeedsDisplay];
}
}

- (void)setStarBorderWidth:(CGFloat)startBorderWidth {
if(startBorderWidth < 0){
startBorderWidth = 0;
}

_starBorderWidth = startBorderWidth;
[self setNeedsDisplay];
}


- (BOOL)shouldUseImages {
return (self.emptyStarImage!=nil && self.filledStarImage!=nil);
}
Expand Down Expand Up @@ -245,15 +283,18 @@ - (void)_drawAccurateHalfStarShapeWithFrame:(CGRect)frame tintColor:(UIColor *)t
[clipPath appendPath:[UIBezierPath bezierPathWithRect:rightRectOfStar]];
clipPath.usesEvenOddFillRule = YES;

[_emptyStarColor setFill];
[starShapePath fill];

CGContextSaveGState(UIGraphicsGetCurrentContext()); {
[clipPath addClip];
[tintColor setFill];
[_filledStarColor setFill];
[starShapePath fill];
}
CGContextRestoreGState(UIGraphicsGetCurrentContext());

[tintColor setStroke];
starShapePath.lineWidth = 1;
[_starBorderColor setStroke];
starShapePath.lineWidth = _starBorderWidth;
[starShapePath stroke];
}

Expand All @@ -267,6 +308,8 @@ - (void)drawRect:(CGRect)rect {
CGFloat availableWidth = rect.size.width - (_spacing * (_maximumValue - 1)) - 2;
CGFloat cellWidth = (availableWidth / _maximumValue);
CGFloat starSide = (cellWidth <= rect.size.height) ? cellWidth : rect.size.height;
starSide = (self.shouldUseImages)? starSide : (starSide - _starBorderWidth);

for (int idx = 0; idx < _maximumValue; idx++) {
CGPoint center = CGPointMake(cellWidth*idx + cellWidth/2 + _spacing*idx + 1, rect.size.height/2);
CGRect frame = CGRectMake(center.x - starSide/2, center.y - starSide/2, starSide, starSide);
Expand Down

0 comments on commit ff8935e

Please sign in to comment.