Skip to content

Instantly share code, notes, and snippets.

@lacyrhoades
Created February 2, 2014 20:05
Show Gist options
  • Save lacyrhoades/8774009 to your computer and use it in GitHub Desktop.
Save lacyrhoades/8774009 to your computer and use it in GitHub Desktop.
//
// EtsyStarsView.m
// Etsy
//
// Created by Lacy Rhoades on 1/30/14.
// Copyright (c) 2014 Etsy. All rights reserved.
//
#import "EtsyStarsView.h"
CGFloat const static EtsyStarWidth = 18.0f;
CGFloat const static EtsyStarSpace = 4.0f;
@interface EtsyStarsView ()
@property (strong) NSArray *starViews;
@property (strong) UIImage *filledStar;
@property (strong) UIImage *emptyStar;
@end
@implementation EtsyStarsView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSMutableArray *starViews = [[NSMutableArray alloc] init];
self.emptyStar = [UIImage imageNamed:@"icon_star_empty.png"];
self.filledStar = [UIImage imageNamed:@"icon_star_filled.png"];
for (int idx = 0; idx < 5; idx++) {
UIImageView *star = [[UIImageView alloc] init];
[starViews addObject:star];
[star setImage:self.emptyStar];
star.frame = CGRectMake(idx * (EtsyStarWidth + EtsyStarSpace), 0.0, EtsyStarWidth, EtsyStarWidth);
[self addSubview:star];
}
self.starViews = starViews;
}
return self;
}
- (void)setStars:(NSInteger)stars {
_stars = stars;
[self.starViews enumerateObjectsUsingBlock:^(UIImageView *star, NSUInteger idx, BOOL *stop) {
star.image = (stars > idx) ? self.filledStar : self.emptyStar;
}];
}
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(EtsyStarWidth * 5 + EtsyStarSpace * 3, EtsyStarWidth);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment