Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SAVBarView.m
- // HappinessIndex
- //
- // Created by Robert Evans on 11/25/08.
- // Copyright 2008 South And Valley. All rights reserved.
- //
- #import "SAVBarView.h"
- static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
- {
- float fw, fh;
- if (ovalWidth == 0 || ovalHeight == 0) {
- CGContextAddRect(context, rect);
- return;
- }
- CGContextSaveGState(context);
- CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
- CGContextScaleCTM (context, ovalWidth, ovalHeight);
- fw = CGRectGetWidth (rect) / ovalWidth;
- fh = CGRectGetHeight (rect) / ovalHeight;
- CGContextMoveToPoint(context, fw, fh/2);
- CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
- CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
- CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
- CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
- CGContextClosePath(context);
- CGContextRestoreGState(context);
- }
- @implementation SAVBarView
- - (id)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
- }
- - (void)drawRect:(CGRect)rect {
- CGContextRef context = UIGraphicsGetCurrentContext();
- //CGContextStrokeRectWithWidth(context, rect, 1.0);
- // Screen 320x480
- // status bar is 20 px.
- // Tabbar 49 px ?? maybe
- //
- // Each happi unit 43.1
- // !!!:robevans:20081125 FontSize ought to be a variable.
- //float happiUnit = 43.1;
- float fontSize = 24.0f;
- // {0, 387} , {24,24}
- CGRect labelRect= {{rect.origin.x, rect.size.height - fontSize }, {fontSize, fontSize}};
- UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
- label.autoresizesSubviews = YES;
- label.textAlignment = UITextAlignmentCenter;
- label.font = [UIFont fontWithName:@"Helvetica" size:fontSize];
- label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
- label.text = @"10";
- label.textColor = [UIColor orangeColor];
- label.shadowColor = [UIColor grayColor];
- label.backgroundColor = [UIColor clearColor];
- label.adjustsFontSizeToFitWidth = TRUE;
- // XXX The height and y coordinate need to be pre calculated
- CGRect barRect = {{rect.origin.x, 0.0f }, {rect.size.width, rect.size.height/2 }};
- [self addRoundRect:barRect radius:6.0f];
- //[self addSubview:[self createBarView:imageRect]];
- [self addSubview:label];
- [label release];
- // SAVBarView will have our total hight but the width will be adjusted.
- //CGRect barRect = CGRectMake(0.0f, 0.0f, 31.0f, rect.size.height);
- //self.frame = rect;
- }
- -(void) addRoundRect:(CGRect)rect radius:(float)radius{
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGSize offset;
- CGColorRef shadowColor;
- //CGContextTranslateCTM(context, 20, 300);
- offset.width = 5;
- offset.height = -5;
- // Set the shadow in the context.
- CGContextSetShadow(context, offset, 0.0);
- // If you were making this as a routine, you would probably accept a rectangle
- // that defines its bounds, and a radius reflecting the "rounded-ness" of the rectangle.
- CGFloat radius1 = 6.0;
- // NOTE: At this point you may want to verify that your radius is no more than half
- // the width and height of your rectangle, as this technique degenerates for those cases.
- // In order to draw a rounded rectangle, we will take advantage of the fact that
- // CGContextAddArcToPoint will draw straight lines past the start and end of the arc
- // in order to create the path from the current position and the destination position.
- // In order to create the 4 arcs correctly, we need to know the min, mid and max positions
- // on the x and y lengths of the given rectangle.
- CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect);
- CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect);
- // Next, we will go around the rectangle in the order given by the figure below.
- // minx midx maxx
- // miny 2 3 4
- // midy 1 9 5
- // maxy 8 7 6
- // Which gives us a coincident start and end point, which is incidental to this technique, but still doesn't
- // form a closed path, so we still need to close the path to connect the ends correctly.
- // Thus we start by moving to point 1, then adding arcs through each pair of points that follows.
- // You could use a similar tecgnique to create any shape with rounded corners.
- // Start at 1
- CGContextMoveToPoint(context, minx, midy);
- // Add an arc through 2 to 3
- CGContextAddArcToPoint(context, minx, miny, midx, miny, radius1);
- // Add an arc through 4 to 5
- CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius1);
- // Add an arc through 6 to 7
- CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius1);
- // Add an arc through 8 to 9
- CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius1);
- // Close the path
- CGContextClosePath(context);
- // Fill & stroke the path
- //CGContextDrawPath(context, kCGPathFillStroke);
- float kWhiteColor[] = { .37265, .46003, .34952, 1.0};
- CGContextSetFillColor(context, kWhiteColor);
- CGContextDrawPath(context, kCGPathFill);
- //CGContextStrokePath(context);
- }
- /* Given a rect, create an image view
- *
- */
- - (UIImageView*)createBarView:(CGRect)rect{
- UIImage *image = [self createBar:rect];
- UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
- // XXX why do we have to do this?
- // imageView.frame = rect;
- return imageView;
- }
- // ought to take a hight value that determines how high the bar will go.
- // should bar.png be hardcoded?
- - (UIImage *)createBar:(CGRect)rect{
- UIImage *img = [UIImage imageNamed:@"bar.png"];
- int w = img.size.width;
- int h = img.size.height;
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
- CGContextBeginPath(context);
- //CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height - unit);
- addRoundedRectToPath(context, rect, 6, 6);
- CGContextClosePath(context);
- CGContextClip(context);
- // XXX setting x is cropping the bottom of the rect.
- CGContextDrawImage(context, CGRectMake(0.0, 85.0, w, h), img.CGImage);
- //CGContextDrawImage(context, rect, img.CGImage);
- CGImageRef imageMasked = CGBitmapContextCreateImage(context);
- CGContextRelease(context);
- CGColorSpaceRelease(colorSpace);
- [img release];
- UIImage *image = [UIImage imageWithCGImage:imageMasked];
- return image;
- }
- - (void)dealloc {
- [super dealloc];
- }
- @end
Add Comment
Please, Sign In to add comment