Today's Menu  Portugal
journal and plan nutrition
DLStarView.m
Go to the documentation of this file.
1 /*
2 
3  DLStarRating
4  Copyright (C) 2011 David Linsin <dlinsin@gmail.com>
5 
6  All rights reserved. This program and the accompanying materials
7  are made available under the terms of the Eclipse Public License v1.0
8  which accompanies this distribution, and is available at
9  http://www.eclipse.org/legal/epl-v10.html
10 
11  */
12 
13 #import "DLStarView.h"
14 #import "DLStarRatingControl.h"
15 
16 @implementation DLStarView
17 
18 #pragma mark -
19 #pragma mark Initialization
20 
21 - (id)initWithDefault:(UIImage*)star highlighted:(UIImage*)highlightedStar position:(int)index allowFractions:(BOOL)fractions {
22  self = [super initWithFrame:CGRectZero];
23 
24  if (self) {
25  [self setTag:index];
26  if (fractions) {
27  highlightedStar = [self croppedImage:highlightedStar];
28  star = [self croppedImage:star];
29  }
30  self.frame = CGRectMake((star.size.width*index), 0, star.size.width, star.size.height+kEdgeInsetBottom);
31  [self setStarImage:star highlightedStarImage:highlightedStar];
32  [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0, kEdgeInsetBottom, 0)];
33  [self setBackgroundColor:[UIColor clearColor]];
34  if (index == 0) {
35  [self setAccessibilityLabel:@"1 star"];
36  } else {
37  [self setAccessibilityLabel:[NSString stringWithFormat:@"%d stars", index+1]];
38  }
39  }
40  return self;
41 }
42 
43 
44 - (UIImage *)croppedImage:(UIImage*)image {
45  float partWidth = image.size.width/kNumberOfFractions * image.scale;
46  int part = (self.tag+kNumberOfFractions)%kNumberOfFractions;
47  float xOffset = partWidth*part;
48  CGRect newFrame = CGRectMake(xOffset, 0, partWidth , image.size.height * image.scale);
49  CGImageRef resultImage = CGImageCreateWithImageInRect([image CGImage], newFrame);
50  UIImage *result = [UIImage imageWithCGImage:resultImage scale:image.scale orientation:image.imageOrientation];
51  CGImageRelease(resultImage);
52  return result;
53 }
54 
55 
56 
57 #pragma mark -
58 #pragma mark UIView methods
59 
60 - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
61  return self.superview;
62 }
63 
64 #pragma mark -
65 #pragma mark Layouting
66 
67 - (void)centerIn:(CGRect)_frame with:(int)numberOfStars {
68  CGSize size = self.frame.size;
69 
70  float height = self.frame.size.height;
71  float frameHeight = _frame.size.height;
72  float newY = (frameHeight-height)/2;
73 
74  float widthOfStars = self.frame.size.width * numberOfStars;
75  float frameWidth = _frame.size.width;
76  float gapToApply = (frameWidth-widthOfStars)/2;
77 
78  self.frame = CGRectMake((size.width*self.tag) + gapToApply, newY, size.width, size.height);
79 }
80 
81 - (void)setStarImage:(UIImage*)starImage highlightedStarImage:(UIImage*)highlightedImage {
82  [self setImage:starImage forState:UIControlStateNormal];
83  [self setImage:highlightedImage forState:UIControlStateSelected];
84  [self setImage:highlightedImage forState:UIControlStateHighlighted];
85 }
86 
87 @end
#define kEdgeInsetBottom
Definition: DLStarView.h:15
void setStarImage:highlightedStarImage:(UIImage *starImage, [highlightedStarImage] UIImage *highlightedImage)
Definition: DLStarView.m:81
#define kNumberOfFractions
UIImage * croppedImage:(UIImage *image)
Definition: DLStarView.m:44