Skip to content

Instantly share code, notes, and snippets.

@Dimillian
Created February 9, 2012 14:00
Show Gist options
  • Save Dimillian/1780152 to your computer and use it in GitHub Desktop.
Save Dimillian/1780152 to your computer and use it in GitHub Desktop.
Rating stars view
- (void)drawRect:(CGRect)rect
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 13, 100, 20)];
[label setFont:[UIFont fontWithName:@"Neutraface Condensed" size:18]];
[label setTextColor:[UIColor colorWithRed:96.0/255.0 green:40.0/255.0 blue:33.0/255.0 alpha:1.0]];
[label setText:@"YOUR RATING"];
[label setBackgroundColor:[UIColor clearColor]];
[self addSubview:label];
CGFloat x = 120;
CGFloat y = 10;
for (int i = 0; i<5; i++){
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(x, y, 23, 23)];
[button setBackgroundColor:[UIColor clearColor]];
[button setImage:[UIImage imageNamed:@"Star_OFF"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"Star_ON"] forState:UIControlStateHighlighted];
[button setTag:i+1];
[button addTarget:self action:@selector(starClicked:) forControlEvents:UIControlEventTouchUpInside];
[_ratingStar addObject:button];
[self addSubview:[_ratingStar objectAtIndex:i]];
x = x + 30;
}
[self setBackgroundColor:[UIColor clearColor]];
}
-(void)starClicked:(id)sender
{
_rating = [sender tag];
for (UIButton *button in _ratingStar){
if (button.tag > _rating) {
[button setImage:[UIImage imageNamed:@"Star_OFF"] forState:UIControlStateNormal];
}
else{
[button setImage:[UIImage imageNamed:@"Star_ON"] forState:UIControlStateNormal];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment