From c151ec3d7831c3ae4ce5c8a9697ef7cb95f58eaa Mon Sep 17 00:00:00 2001 From: Nickolas Pohilets Date: Fri, 19 Aug 2016 11:05:30 +0200 Subject: [PATCH 1/2] Fixed compiler warnings --- HCSStarRatingView/HCSStarRatingView.m | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/HCSStarRatingView/HCSStarRatingView.m b/HCSStarRatingView/HCSStarRatingView.m index 9a3fea8..8dc6273 100644 --- a/HCSStarRatingView/HCSStarRatingView.m +++ b/HCSStarRatingView/HCSStarRatingView.m @@ -47,7 +47,7 @@ - (instancetype)initWithFrame:(CGRect)frame { return self; } -- (id)initWithCoder:(NSCoder *)aDecoder { +- (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self _customInit]; @@ -361,10 +361,7 @@ - (void)_handleTouch:(UITouch *)touch { CGPoint location = [touch locationInView:self]; CGFloat value = location.x / cellWidth; if (_allowsHalfStars) { - if (_accurateHalfStars) { - value = value; - } - else { + if (!_accurateHalfStars) { if (value+.5f < ceilf(value)) { value = floor(value)+.5f; } else { From 9c98fed90a5faab2aed1c1710e379e9a2afab6e3 Mon Sep 17 00:00:00 2001 From: Nickolas Pohilets Date: Fri, 19 Aug 2016 11:24:46 +0200 Subject: [PATCH 2/2] Implemented support for RTL languages --- HCSStarRatingView/HCSStarRatingView.h | 4 ++++ HCSStarRatingView/HCSStarRatingView.m | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/HCSStarRatingView/HCSStarRatingView.h b/HCSStarRatingView/HCSStarRatingView.h index 007ab69..550d058 100644 --- a/HCSStarRatingView/HCSStarRatingView.h +++ b/HCSStarRatingView/HCSStarRatingView.h @@ -42,5 +42,9 @@ IB_DESIGNABLE @property (nonatomic, strong) IBInspectable UIImage *emptyStarImage; @property (nonatomic, strong) IBInspectable UIImage *halfStarImage; @property (nonatomic, strong) IBInspectable UIImage *filledStarImage; + +// If YES, then stars layout respects UIApplication.userInterfaceLayoutDirection +@property (nonatomic) IBInspectable BOOL flipsInRTL; + @end diff --git a/HCSStarRatingView/HCSStarRatingView.m b/HCSStarRatingView/HCSStarRatingView.m index 8dc6273..f81e382 100644 --- a/HCSStarRatingView/HCSStarRatingView.m +++ b/HCSStarRatingView/HCSStarRatingView.m @@ -161,6 +161,13 @@ - (void)setFilledStarImage:(UIImage *)filledStarImage { } } +- (void)setFlipsInRTL:(BOOL)flipsInRTL { + if (_flipsInRTL != flipsInRTL) { + _flipsInRTL = flipsInRTL; + [self setNeedsDisplay]; + } +} + - (BOOL)shouldUseImages { return (self.emptyStarImage!=nil && self.filledStarImage!=nil); } @@ -268,7 +275,8 @@ - (void)drawRect:(CGRect)rect { CGFloat cellWidth = (availableWidth / _maximumValue); CGFloat starSide = (cellWidth <= rect.size.height) ? cellWidth : rect.size.height; for (int idx = 0; idx < _maximumValue; idx++) { - CGPoint center = CGPointMake(cellWidth*idx + cellWidth/2 + _spacing*idx + 1, rect.size.height/2); + NSUInteger positionIndex = [self _shouldFlip] ? _maximumValue - idx - 1 : idx; + CGPoint center = CGPointMake(cellWidth*positionIndex + cellWidth/2 + _spacing*positionIndex + 1, rect.size.height/2); CGRect frame = CGRectMake(center.x - starSide/2, center.y - starSide/2, starSide, starSide); BOOL highlighted = (idx+1 <= ceilf(_value)); if (_allowsHalfStars && highlighted && (idx+1 > _value)) { @@ -284,6 +292,10 @@ - (void)drawRect:(CGRect)rect { } } +- (BOOL)_shouldFlip { + return _flipsInRTL && [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft; +} + - (void)_drawStarWithFrame:(CGRect)frame tintColor:(UIColor *)tintColor highlighted:(BOOL)highlighted { if (self.shouldUseImages) { [self _drawStarImageWithFrame:frame tintColor:tintColor highlighted:highlighted]; @@ -299,6 +311,7 @@ - (void)_drawHalfStarWithFrame:(CGRect)frame tintColor:(UIColor *)tintColor { [self _drawHalfStarShapeWithFrame:frame tintColor:tintColor]; } } + - (void)_drawAccurateStarWithFrame:(CGRect)frame tintColor:(UIColor *)tintColor progress:(CGFloat)progress { if (self.shouldUseImages) { [self _drawAccurateHalfStarImageWithFrame:frame tintColor:tintColor progress:progress]; @@ -357,9 +370,12 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { } - (void)_handleTouch:(UITouch *)touch { - CGFloat cellWidth = self.bounds.size.width / _maximumValue; CGPoint location = [touch locationInView:self]; - CGFloat value = location.x / cellWidth; + CGFloat value = _maximumValue * (location.x / self.bounds.size.width); + if ([self _shouldFlip]) { + value = _maximumValue - value; + } + if (_allowsHalfStars) { if (!_accurateHalfStars) { if (value+.5f < ceilf(value)) {