StyledTableViewCell.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * Copyright (c) 2012 Muh Hon Cheng
  3. * Created by honcheng on 28/2/12.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject
  11. * to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
  17. * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  18. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  21. * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  25. * IN CONNECTION WITH THE SOFTWARE OR
  26. * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. * @author Muh Hon Cheng <honcheng@gmail.com>
  29. * @copyright 2012 Muh Hon Cheng
  30. * @version
  31. *
  32. */
  33. #import "StyledTableViewCell.h"
  34. #import <QuartzCore/QuartzCore.h>
  35. @implementation StyledTableViewCellBackgroundView
  36. - (BOOL) isOpaque
  37. {
  38. return YES;
  39. }
  40. - (id)initWithFrame:(CGRect)frame
  41. {
  42. if (self = [super initWithFrame:frame])
  43. {
  44. [self setBackgroundColor:[UIColor whiteColor]];
  45. }
  46. return self;
  47. }
  48. - (void)drawRect:(CGRect)rect
  49. {
  50. // if no separator color is set, use this
  51. if (!self.separatorColor)
  52. {
  53. [self setSeparatorColor:[UIColor colorWithRed:190/255.0 green:183/255.0 blue:145/255.0 alpha:1]];
  54. }
  55. CGContextRef c = UIGraphicsGetCurrentContext();
  56. CGContextSetStrokeColorWithColor(c, [_separatorColor CGColor]);
  57. CGContextSetLineWidth(c, self.dashStroke);
  58. // if gap==0, draw a continuous line
  59. if (self.dashGap>0)
  60. {
  61. float dash[2] = { self.dashWidth , self.dashGap};
  62. CGContextSetLineDash(c,0,(const)dash,2);
  63. }
  64. CGContextBeginPath(c);
  65. CGContextMoveToPoint(c, 0.0f, rect.size.height-self.dashStroke/2);
  66. CGContextAddLineToPoint(c, rect.size.width, rect.size.height-self.dashStroke/2);
  67. CGContextStrokePath(c);
  68. }
  69. @end
  70. @interface StyledTableViewCellSelectedBackgroundView()
  71. @property (nonatomic, assign) float prevLayerHeight;
  72. @end
  73. @implementation StyledTableViewCellSelectedBackgroundView
  74. - (void)drawRect:(CGRect)rect
  75. {
  76. if (self.frame.size.height!=self.prevLayerHeight)
  77. {
  78. for (int i=0; i<[self.layer.sublayers count]; i++)
  79. {
  80. id layer = [self.layer.sublayers objectAtIndex:i];
  81. if ([layer isKindOfClass:[CAGradientLayer class]])
  82. {
  83. [layer removeFromSuperlayer];
  84. }
  85. }
  86. }
  87. if (!self.selectedBackgroundGradientColors)
  88. {
  89. // use this color if no gradient color exists
  90. self.selectedBackgroundGradientColors = @[(id)[[UIColor colorWithWhite:0.9 alpha:1] CGColor],(id)[[UIColor colorWithWhite:0.95 alpha:1] CGColor]];
  91. }
  92. else if ([self.selectedBackgroundGradientColors count]==1)
  93. {
  94. // at least 2 colors are required to set gradient
  95. // if only one color provided, use the same color for both extremes of the gradient
  96. self.selectedBackgroundGradientColors = @[[self.selectedBackgroundGradientColors objectAtIndex:0],[self.selectedBackgroundGradientColors objectAtIndex:0]];
  97. }
  98. // draw the selected background gradient
  99. CAGradientLayer *gradient = [CAGradientLayer layer];
  100. [gradient setFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height-self.dashStroke)];
  101. if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionVertical)
  102. {
  103. [gradient setStartPoint:CGPointMake(0, 0)];
  104. [gradient setEndPoint:CGPointMake(0, 1)];
  105. }
  106. else if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionHorizontal)
  107. {
  108. [gradient setStartPoint:CGPointMake(0, 0)];
  109. [gradient setEndPoint:CGPointMake(1, 0)];
  110. }
  111. else if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionDiagonalTopLeftToBottomRight)
  112. {
  113. [gradient setStartPoint:CGPointMake(0, 0)];
  114. [gradient setEndPoint:CGPointMake(1, 1)];
  115. }
  116. else if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionDiagonalBottomLeftToTopRight)
  117. {
  118. [gradient setStartPoint:CGPointMake(0, 1)];
  119. [gradient setEndPoint:CGPointMake(1, 0)];
  120. }
  121. [self.layer insertSublayer:gradient atIndex:0];
  122. [gradient setColors:self.selectedBackgroundGradientColors];
  123. [super drawRect:rect];
  124. self.prevLayerHeight = self.frame.size.height;
  125. }
  126. @end
  127. @implementation StyledTableViewCell
  128. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  129. {
  130. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  131. if (self) {
  132. // set the background view
  133. StyledTableViewCellBackgroundView *backgroundView = [[StyledTableViewCellBackgroundView alloc] initWithFrame:CGRectZero];
  134. [self setBackgroundView:backgroundView];
  135. // set the selected background view
  136. StyledTableViewCellSelectedBackgroundView *selectedBackgroundView = [[StyledTableViewCellSelectedBackgroundView alloc] initWithFrame:CGRectZero];
  137. [self setSelectedBackgroundView:selectedBackgroundView];
  138. // clear the background color of text label because text label background overlaps with separator
  139. [self.textLabel setBackgroundColor:[UIColor clearColor]];
  140. [self setDashWidth:1 dashGap:0 dashStroke:1];
  141. }
  142. return self;
  143. }
  144. - (void)layoutSubviews
  145. {
  146. [super layoutSubviews];
  147. [self.selectedBackgroundView setNeedsDisplay];
  148. }
  149. // set the selected background color by providing an array of colors
  150. // requires a list of CGColor
  151. - (void)setSelectedBackgroundViewGradientColors:(NSArray*)colors
  152. {
  153. [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setSelectedBackgroundGradientColors:colors];
  154. }
  155. // set the selected background color using style
  156. - (void)setStyledTableViewCellSelectionStyle:(StyledTableViewCellSelectionStyle)style
  157. {
  158. _styledTableViewCellSelectionStyle = style;
  159. NSMutableArray *colors = [NSMutableArray array];
  160. if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStyleCyan)
  161. {
  162. [colors addObject:(id)[[UIColor colorWithRed:134/255.0 green:214/255.0 blue:231/255.0 alpha:1] CGColor]];
  163. [colors addObject:(id)[[UIColor colorWithRed:111/255.0 green:198/255.0 blue:217/255.0 alpha:1] CGColor]];
  164. }
  165. else if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStyleGreen)
  166. {
  167. [colors addObject:(id)[[UIColor colorWithRed:124/255.0 green:243/255.0 blue:127/255.0 alpha:1] CGColor]];
  168. [colors addObject:(id)[[UIColor colorWithRed:111/255.0 green:222/255.0 blue:114/255.0 alpha:1] CGColor]];
  169. }
  170. else if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStyleYellow)
  171. {
  172. [colors addObject:(id)[[UIColor colorWithRed:248/255.0 green:242/255.0 blue:145/255.0 alpha:1] CGColor]];
  173. [colors addObject:(id)[[UIColor colorWithRed:243/255.0 green:236/255.0 blue:124/255.0 alpha:1] CGColor]];
  174. }
  175. else if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStylePurple)
  176. {
  177. [colors addObject:(id)[[UIColor colorWithRed:217/255.0 green:143/255.0 blue:230/255.0 alpha:1] CGColor]];
  178. [colors addObject:(id)[[UIColor colorWithRed:190/255.0 green:110/255.0 blue:204/255.0 alpha:1] CGColor]];
  179. }
  180. else
  181. {
  182. [colors addObject:(id)[[UIColor colorWithWhite:0.95 alpha:1] CGColor]];
  183. [colors addObject:(id)[[UIColor colorWithWhite:0.9 alpha:1] CGColor]];
  184. }
  185. [self setSelectedBackgroundViewGradientColors:colors];
  186. }
  187. // set the selected background color gradient direction
  188. - (void)setSelectionGradientDirection:(StyledTableViewCellSelectionGradientDirection)direction
  189. {
  190. [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setGradientDirection:direction];
  191. }
  192. // overrides the method that changes the cell separator color
  193. // there is no need to call this method
  194. - (void)setSeparatorColor:(UIColor*)separatorColor
  195. {
  196. [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setSeparatorColor:separatorColor];
  197. [(StyledTableViewCellBackgroundView*)self.backgroundView setSeparatorColor:separatorColor];
  198. }
  199. // set the separator property including width, gap and stroke width
  200. - (void)setDashWidth:(int)dashWidth dashGap:(int)dashGap dashStroke:(int)dashStroke
  201. {
  202. [self setDashWidth:dashWidth];
  203. [self setDashGap:dashGap];
  204. [self setDashStroke:dashStroke];
  205. }
  206. // set the separator dash gap
  207. // separator is a continuous line if gap is 0
  208. - (void)setDashGap:(int)dashGap
  209. {
  210. _dashGap = dashGap;
  211. [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setDashGap:self.dashGap];
  212. [(StyledTableViewCellBackgroundView*)self.backgroundView setDashGap:self.dashGap];
  213. }
  214. // set the separator stroke width
  215. - (void)setDashStroke:(int)dashStroke
  216. {
  217. _dashStroke = dashStroke;
  218. [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setDashStroke:self.dashStroke];
  219. [(StyledTableViewCellBackgroundView*)self.backgroundView setDashStroke:self.dashStroke];
  220. }
  221. // set the separator dash width
  222. - (void)setDashWidth:(int)dashWidth
  223. {
  224. _dashWidth = dashWidth;
  225. [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setDashWidth:self.dashWidth];
  226. [(StyledTableViewCellBackgroundView*)self.backgroundView setDashWidth:self.dashWidth];
  227. }
  228. @end