123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- #import "StyledTableViewCell.h"
- #import <QuartzCore/QuartzCore.h>
- @implementation StyledTableViewCellBackgroundView
- - (BOOL) isOpaque
- {
- return YES;
- }
- - (id)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame])
- {
- [self setBackgroundColor:[UIColor whiteColor]];
- }
- return self;
- }
- - (void)drawRect:(CGRect)rect
- {
-
- if (!self.separatorColor)
- {
- [self setSeparatorColor:[UIColor colorWithRed:190/255.0 green:183/255.0 blue:145/255.0 alpha:1]];
- }
-
- CGContextRef c = UIGraphicsGetCurrentContext();
- CGContextSetStrokeColorWithColor(c, [_separatorColor CGColor]);
- CGContextSetLineWidth(c, self.dashStroke);
-
-
- if (self.dashGap>0)
- {
- float dash[2] = { self.dashWidth , self.dashGap};
- CGContextSetLineDash(c,0,(const)dash,2);
- }
-
- CGContextBeginPath(c);
- CGContextMoveToPoint(c, 0.0f, rect.size.height-self.dashStroke/2);
- CGContextAddLineToPoint(c, rect.size.width, rect.size.height-self.dashStroke/2);
- CGContextStrokePath(c);
- }
- @end
- @interface StyledTableViewCellSelectedBackgroundView()
- @property (nonatomic, assign) float prevLayerHeight;
- @end
- @implementation StyledTableViewCellSelectedBackgroundView
- - (void)drawRect:(CGRect)rect
- {
- if (self.frame.size.height!=self.prevLayerHeight)
- {
- for (int i=0; i<[self.layer.sublayers count]; i++)
- {
- id layer = [self.layer.sublayers objectAtIndex:i];
- if ([layer isKindOfClass:[CAGradientLayer class]])
- {
- [layer removeFromSuperlayer];
- }
- }
- }
-
- if (!self.selectedBackgroundGradientColors)
- {
-
- self.selectedBackgroundGradientColors = @[(id)[[UIColor colorWithWhite:0.9 alpha:1] CGColor],(id)[[UIColor colorWithWhite:0.95 alpha:1] CGColor]];
- }
- else if ([self.selectedBackgroundGradientColors count]==1)
- {
-
-
- self.selectedBackgroundGradientColors = @[[self.selectedBackgroundGradientColors objectAtIndex:0],[self.selectedBackgroundGradientColors objectAtIndex:0]];
- }
-
-
- CAGradientLayer *gradient = [CAGradientLayer layer];
- [gradient setFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height-self.dashStroke)];
- if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionVertical)
- {
- [gradient setStartPoint:CGPointMake(0, 0)];
- [gradient setEndPoint:CGPointMake(0, 1)];
- }
- else if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionHorizontal)
- {
- [gradient setStartPoint:CGPointMake(0, 0)];
- [gradient setEndPoint:CGPointMake(1, 0)];
- }
- else if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionDiagonalTopLeftToBottomRight)
- {
- [gradient setStartPoint:CGPointMake(0, 0)];
- [gradient setEndPoint:CGPointMake(1, 1)];
- }
- else if (self.gradientDirection==StyledTableViewCellSelectionGradientDirectionDiagonalBottomLeftToTopRight)
- {
- [gradient setStartPoint:CGPointMake(0, 1)];
- [gradient setEndPoint:CGPointMake(1, 0)];
- }
- [self.layer insertSublayer:gradient atIndex:0];
- [gradient setColors:self.selectedBackgroundGradientColors];
-
- [super drawRect:rect];
-
- self.prevLayerHeight = self.frame.size.height;
- }
- @end
- @implementation StyledTableViewCell
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
-
-
- StyledTableViewCellBackgroundView *backgroundView = [[StyledTableViewCellBackgroundView alloc] initWithFrame:CGRectZero];
- [self setBackgroundView:backgroundView];
-
-
- StyledTableViewCellSelectedBackgroundView *selectedBackgroundView = [[StyledTableViewCellSelectedBackgroundView alloc] initWithFrame:CGRectZero];
- [self setSelectedBackgroundView:selectedBackgroundView];
-
-
- [self.textLabel setBackgroundColor:[UIColor clearColor]];
- [self setDashWidth:1 dashGap:0 dashStroke:1];
- }
- return self;
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- [self.selectedBackgroundView setNeedsDisplay];
- }
- - (void)setSelectedBackgroundViewGradientColors:(NSArray*)colors
- {
- [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setSelectedBackgroundGradientColors:colors];
- }
- - (void)setStyledTableViewCellSelectionStyle:(StyledTableViewCellSelectionStyle)style
- {
- _styledTableViewCellSelectionStyle = style;
-
- NSMutableArray *colors = [NSMutableArray array];
- if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStyleCyan)
- {
- [colors addObject:(id)[[UIColor colorWithRed:134/255.0 green:214/255.0 blue:231/255.0 alpha:1] CGColor]];
- [colors addObject:(id)[[UIColor colorWithRed:111/255.0 green:198/255.0 blue:217/255.0 alpha:1] CGColor]];
- }
- else if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStyleGreen)
- {
- [colors addObject:(id)[[UIColor colorWithRed:124/255.0 green:243/255.0 blue:127/255.0 alpha:1] CGColor]];
- [colors addObject:(id)[[UIColor colorWithRed:111/255.0 green:222/255.0 blue:114/255.0 alpha:1] CGColor]];
- }
- else if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStyleYellow)
- {
- [colors addObject:(id)[[UIColor colorWithRed:248/255.0 green:242/255.0 blue:145/255.0 alpha:1] CGColor]];
- [colors addObject:(id)[[UIColor colorWithRed:243/255.0 green:236/255.0 blue:124/255.0 alpha:1] CGColor]];
- }
- else if (_styledTableViewCellSelectionStyle==StyledTableViewCellSelectionStylePurple)
- {
- [colors addObject:(id)[[UIColor colorWithRed:217/255.0 green:143/255.0 blue:230/255.0 alpha:1] CGColor]];
- [colors addObject:(id)[[UIColor colorWithRed:190/255.0 green:110/255.0 blue:204/255.0 alpha:1] CGColor]];
- }
- else
- {
- [colors addObject:(id)[[UIColor colorWithWhite:0.95 alpha:1] CGColor]];
- [colors addObject:(id)[[UIColor colorWithWhite:0.9 alpha:1] CGColor]];
- }
- [self setSelectedBackgroundViewGradientColors:colors];
- }
- - (void)setSelectionGradientDirection:(StyledTableViewCellSelectionGradientDirection)direction
- {
- [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setGradientDirection:direction];
- }
- - (void)setSeparatorColor:(UIColor*)separatorColor
- {
- [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setSeparatorColor:separatorColor];
- [(StyledTableViewCellBackgroundView*)self.backgroundView setSeparatorColor:separatorColor];
- }
- - (void)setDashWidth:(int)dashWidth dashGap:(int)dashGap dashStroke:(int)dashStroke
- {
- [self setDashWidth:dashWidth];
- [self setDashGap:dashGap];
- [self setDashStroke:dashStroke];
- }
- - (void)setDashGap:(int)dashGap
- {
- _dashGap = dashGap;
- [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setDashGap:self.dashGap];
- [(StyledTableViewCellBackgroundView*)self.backgroundView setDashGap:self.dashGap];
- }
- - (void)setDashStroke:(int)dashStroke
- {
- _dashStroke = dashStroke;
- [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setDashStroke:self.dashStroke];
- [(StyledTableViewCellBackgroundView*)self.backgroundView setDashStroke:self.dashStroke];
- }
- - (void)setDashWidth:(int)dashWidth
- {
- _dashWidth = dashWidth;
- [(StyledTableViewCellSelectedBackgroundView*)self.selectedBackgroundView setDashWidth:self.dashWidth];
- [(StyledTableViewCellBackgroundView*)self.backgroundView setDashWidth:self.dashWidth];
- }
- @end
|