Skip to content
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
4 changes: 4 additions & 0 deletions HCSStarRatingView/HCSStarRatingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

29 changes: 21 additions & 8 deletions HCSStarRatingView/HCSStarRatingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -357,14 +370,14 @@ - (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) {
value = value;
}
else {
if (!_accurateHalfStars) {
if (value+.5f < ceilf(value)) {
value = floor(value)+.5f;
} else {
Expand Down