From 1d8c8734a3d83580e3dd3d74987ab73922f6a1b0 Mon Sep 17 00:00:00 2001 From: Irfan Gul Date: Mon, 7 Nov 2016 18:45:14 +0500 Subject: [PATCH] Added getter of BorderColor and code refactor --- HCSStarRatingView/HCSStarRatingView.h | 1 - HCSStarRatingView/HCSStarRatingView.m | 41 ++++++++++++--------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/HCSStarRatingView/HCSStarRatingView.h b/HCSStarRatingView/HCSStarRatingView.h index d191886..04e483b 100644 --- a/HCSStarRatingView/HCSStarRatingView.h +++ b/HCSStarRatingView/HCSStarRatingView.h @@ -43,7 +43,6 @@ IB_DESIGNABLE @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; diff --git a/HCSStarRatingView/HCSStarRatingView.m b/HCSStarRatingView/HCSStarRatingView.m index 5581261..4e9ad8c 100644 --- a/HCSStarRatingView/HCSStarRatingView.m +++ b/HCSStarRatingView/HCSStarRatingView.m @@ -30,12 +30,14 @@ @implementation HCSStarRatingView { CGFloat _minimumValue; NSUInteger _maximumValue; CGFloat _value; + UIColor *_starBorderColor; } @dynamic minimumValue; @dynamic maximumValue; @dynamic value; @dynamic shouldUseImages; +@dynamic starBorderColor; #pragma mark - Initialization @@ -63,10 +65,7 @@ - (void)_customInit { _spacing = 5.f; _continuous = YES; _starBorderWidth = 1.0f; - _starBorderColor = self.tintColor; _emptyStarColor = [UIColor clearColor]; - _filledStarColor = self.tintColor; - [self _updateAppearanceForState:self.enabled]; } @@ -167,34 +166,30 @@ - (void)setFilledStarImage:(UIImage *)filledStarImage { } } - -- (void)setFilledStarColor:(UIColor *)filledStarColor { - if(_filledStarColor != filledStarColor) { - _filledStarColor = filledStarColor; - [self setNeedsDisplay]; - } -} - - (void)setEmptyStarColor:(UIColor *)emptyStarColor { - if(_emptyStarColor != emptyStarColor) { + if (_emptyStarColor != emptyStarColor) { _emptyStarColor = emptyStarColor; [self setNeedsDisplay]; } } -- (void)setStarBorderColor:(UIColor *)startBorderColor { - if(_starBorderColor != startBorderColor) { - _starBorderColor = startBorderColor; +- (void)setStarBorderColor:(UIColor *)starBorderColor { + if (_starBorderColor != starBorderColor) { + _starBorderColor = starBorderColor; [self setNeedsDisplay]; } } -- (void)setStarBorderWidth:(CGFloat)startBorderWidth { - if(startBorderWidth < 0){ - startBorderWidth = 0; +- (UIColor *)starBorderColor { + if (_starBorderColor == nil) { + return self.tintColor; + } else { + return _starBorderColor; } - - _starBorderWidth = startBorderWidth; +} + +- (void)setStarBorderWidth:(CGFloat)starBorderWidth { + _starBorderWidth = MAX(0, starBorderWidth); [self setNeedsDisplay]; } @@ -288,12 +283,12 @@ - (void)_drawAccurateHalfStarShapeWithFrame:(CGRect)frame tintColor:(UIColor *)t CGContextSaveGState(UIGraphicsGetCurrentContext()); { [clipPath addClip]; - [_filledStarColor setFill]; + [tintColor setFill]; [starShapePath fill]; } CGContextRestoreGState(UIGraphicsGetCurrentContext()); - [_starBorderColor setStroke]; + [self.starBorderColor setStroke]; starShapePath.lineWidth = _starBorderWidth; [starShapePath stroke]; } @@ -308,7 +303,7 @@ - (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); + 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);