Guest User

Untitled

a guest
May 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. //
  2. // SAVBarView.m
  3. // HappinessIndex
  4. //
  5. // Created by Robert Evans on 11/25/08.
  6. // Copyright 2008 South And Valley. All rights reserved.
  7. //
  8.  
  9. #import "SAVBarView.h"
  10.  
  11. static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
  12. {
  13. float fw, fh;
  14. if (ovalWidth == 0 || ovalHeight == 0) {
  15. CGContextAddRect(context, rect);
  16. return;
  17. }
  18. CGContextSaveGState(context);
  19. CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
  20. CGContextScaleCTM (context, ovalWidth, ovalHeight);
  21. fw = CGRectGetWidth (rect) / ovalWidth;
  22. fh = CGRectGetHeight (rect) / ovalHeight;
  23. CGContextMoveToPoint(context, fw, fh/2);
  24. CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
  25. CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
  26. CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
  27. CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
  28. CGContextClosePath(context);
  29. CGContextRestoreGState(context);
  30. }
  31.  
  32. @implementation SAVBarView
  33.  
  34.  
  35. - (id)initWithFrame:(CGRect)frame {
  36. if (self = [super initWithFrame:frame]) {
  37. self.backgroundColor = [UIColor clearColor];
  38. }
  39. return self;
  40. }
  41.  
  42.  
  43. - (void)drawRect:(CGRect)rect {
  44. CGContextRef context = UIGraphicsGetCurrentContext();
  45. //CGContextStrokeRectWithWidth(context, rect, 1.0);
  46. // Screen 320x480
  47. // status bar is 20 px.
  48. // Tabbar 49 px ?? maybe
  49. //
  50. // Each happi unit 43.1
  51.  
  52. // !!!:robevans:20081125 FontSize ought to be a variable.
  53. //float happiUnit = 43.1;
  54. float fontSize = 24.0f;
  55. // {0, 387} , {24,24}
  56. CGRect labelRect= {{rect.origin.x, rect.size.height - fontSize }, {fontSize, fontSize}};
  57.  
  58. UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
  59. label.autoresizesSubviews = YES;
  60. label.textAlignment = UITextAlignmentCenter;
  61. label.font = [UIFont fontWithName:@"Helvetica" size:fontSize];
  62. label.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  63. label.text = @"10";
  64. label.textColor = [UIColor orangeColor];
  65. label.shadowColor = [UIColor grayColor];
  66. label.backgroundColor = [UIColor clearColor];
  67. label.adjustsFontSizeToFitWidth = TRUE;
  68.  
  69. // XXX The height and y coordinate need to be pre calculated
  70. CGRect barRect = {{rect.origin.x, 0.0f }, {rect.size.width, rect.size.height/2 }};
  71.  
  72.  
  73. [self addRoundRect:barRect radius:6.0f];
  74. //[self addSubview:[self createBarView:imageRect]];
  75. [self addSubview:label];
  76. [label release];
  77.  
  78. // SAVBarView will have our total hight but the width will be adjusted.
  79. //CGRect barRect = CGRectMake(0.0f, 0.0f, 31.0f, rect.size.height);
  80. //self.frame = rect;
  81. }
  82.  
  83. -(void) addRoundRect:(CGRect)rect radius:(float)radius{
  84. CGContextRef context = UIGraphicsGetCurrentContext();
  85.  
  86.  
  87.  
  88. CGSize offset;
  89. CGColorRef shadowColor;
  90. //CGContextTranslateCTM(context, 20, 300);
  91.  
  92. offset.width = 5;
  93. offset.height = -5;
  94. // Set the shadow in the context.
  95. CGContextSetShadow(context, offset, 0.0);
  96.  
  97.  
  98. // If you were making this as a routine, you would probably accept a rectangle
  99. // that defines its bounds, and a radius reflecting the "rounded-ness" of the rectangle.
  100. CGFloat radius1 = 6.0;
  101. // NOTE: At this point you may want to verify that your radius is no more than half
  102. // the width and height of your rectangle, as this technique degenerates for those cases.
  103.  
  104. // In order to draw a rounded rectangle, we will take advantage of the fact that
  105. // CGContextAddArcToPoint will draw straight lines past the start and end of the arc
  106. // in order to create the path from the current position and the destination position.
  107.  
  108. // In order to create the 4 arcs correctly, we need to know the min, mid and max positions
  109. // on the x and y lengths of the given rectangle.
  110. CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect);
  111. CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect);
  112.  
  113. // Next, we will go around the rectangle in the order given by the figure below.
  114. // minx midx maxx
  115. // miny 2 3 4
  116. // midy 1 9 5
  117. // maxy 8 7 6
  118. // Which gives us a coincident start and end point, which is incidental to this technique, but still doesn't
  119. // form a closed path, so we still need to close the path to connect the ends correctly.
  120. // Thus we start by moving to point 1, then adding arcs through each pair of points that follows.
  121. // You could use a similar tecgnique to create any shape with rounded corners.
  122.  
  123. // Start at 1
  124. CGContextMoveToPoint(context, minx, midy);
  125. // Add an arc through 2 to 3
  126. CGContextAddArcToPoint(context, minx, miny, midx, miny, radius1);
  127. // Add an arc through 4 to 5
  128. CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius1);
  129. // Add an arc through 6 to 7
  130. CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius1);
  131. // Add an arc through 8 to 9
  132. CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius1);
  133. // Close the path
  134. CGContextClosePath(context);
  135. // Fill & stroke the path
  136.  
  137.  
  138. //CGContextDrawPath(context, kCGPathFillStroke);
  139. float kWhiteColor[] = { .37265, .46003, .34952, 1.0};
  140. CGContextSetFillColor(context, kWhiteColor);
  141.  
  142. CGContextDrawPath(context, kCGPathFill);
  143. //CGContextStrokePath(context);
  144.  
  145. }
  146.  
  147.  
  148.  
  149. /* Given a rect, create an image view
  150. *
  151. */
  152. - (UIImageView*)createBarView:(CGRect)rect{
  153.  
  154. UIImage *image = [self createBar:rect];
  155. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  156.  
  157. // XXX why do we have to do this?
  158. // imageView.frame = rect;
  159. return imageView;
  160. }
  161. // ought to take a hight value that determines how high the bar will go.
  162. // should bar.png be hardcoded?
  163. - (UIImage *)createBar:(CGRect)rect{
  164.  
  165.  
  166. UIImage *img = [UIImage imageNamed:@"bar.png"];
  167. int w = img.size.width;
  168. int h = img.size.height;
  169.  
  170. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  171. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  172.  
  173.  
  174. CGContextBeginPath(context);
  175.  
  176. //CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height - unit);
  177.  
  178. addRoundedRectToPath(context, rect, 6, 6);
  179. CGContextClosePath(context);
  180. CGContextClip(context);
  181.  
  182.  
  183. // XXX setting x is cropping the bottom of the rect.
  184. CGContextDrawImage(context, CGRectMake(0.0, 85.0, w, h), img.CGImage);
  185. //CGContextDrawImage(context, rect, img.CGImage);
  186.  
  187. CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  188. CGContextRelease(context);
  189. CGColorSpaceRelease(colorSpace);
  190. [img release];
  191.  
  192. UIImage *image = [UIImage imageWithCGImage:imageMasked];
  193.  
  194. return image;
  195. }
  196.  
  197.  
  198.  
  199.  
  200. - (void)dealloc {
  201. [super dealloc];
  202. }
  203.  
  204.  
  205. @end
Add Comment
Please, Sign In to add comment