-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRTableViewCell.m
executable file
·176 lines (147 loc) · 6.53 KB
/
CRTableViewCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// CRTableViewCell.m
// CRMultiRowSelector
//
// Created by Christian Roman Mendoza on 6/17/12.
// Copyright (c) 2012 chroman. All rights reserved.
//
#import "CRTableViewCell.h"
/* Macro for background colors */
#define colorWithRGBHex(hex)[UIColor colorWithRed:((float)((hex&0xFF0000)>>16))/255.0 green:((float)((hex&0xFF00)>>8))/255.0 blue:((float)(hex&0xFF))/255.0 alpha:1.0]
#define clearColorWithRGBHex(hex)[UIColor colorWithRed:MIN((((int)(hex>>16)&0xFF)/255.0)+.1,1.0)green:MIN((((int)(hex>>8)&0xFF)/255.0)+.1,1.0)blue:MIN((((int)(hex)&0xFF)/255.0)+.1,1.0)alpha:1.0]
/* Unselected mark constants */
#define kCircleRadioUnselected 23.0
#define kCircleLeftMargin 13.0
#define kCircleRect CGRectMake(3.5, 2.5, 22.0, 22.0)
#define kCircleOverlayRect CGRectMake(1.5, 12.5, 26.0, 23.0)
/* Mark constants */
#define kStrokeWidth 2.0
#define kShadowRadius 4.0
#define kMarkDegrees 70.0
#define kMarkWidth 3.0
#define kMarkHeight 6.0
#define kShadowOffset CGSizeMake(.0, 2.0)
#define kMarkShadowOffset CGSizeMake(.0, -1.0)
#define kMarkImageSize CGSizeMake(30.0, 30.0)
#define kMarkBase CGPointMake(9.0, 13.5)
#define kMarkDrawPoint CGPointMake(20.0, 9.5)
#define kShadowColor [UIColor colorWithWhite:.0 alpha:0.7]
#define kMarkShadowColor [UIColor colorWithWhite:.0 alpha:0.3]
#define kBlueColor 0x236ed8
#define kGreenColor 0x179714
#define kRedColor 0xa4091c
#define kMarkColor kBlueColor
/* Colums and cell constants */
#define kColumnPosition 50.0
#define kMarkCell 60.0
#define kImageRect CGRectMake(10.0, 8.0, 30.0, 30.0)
@implementation CRTableViewCell
@synthesize isSelected = _isSelected;
@synthesize textLabel = label;
@synthesize imageView = imageView;
@synthesize renderedMark = _renderedMark;
- (void)drawRect:(CGRect)rect
{
_isSelected = NO;
CGFloat posY = (rect.size.height / 2) - kCircleRadioUnselected/2;
CGRect unselectedCircleRect = CGRectMake(kCircleLeftMargin, posY, kCircleRadioUnselected, kCircleRadioUnselected);
CGRect imageViewRect = CGRectMake(10, rect.size.height/2 - kCircleLeftMargin - 1, kMarkCell/2, kMarkCell/2);
imageView.frame = imageViewRect; // Center the imageView
UIBezierPath *unselectedCircle = [UIBezierPath bezierPathWithOvalInRect:unselectedCircleRect]; // Unselected circle centered
CGContextRef ctx = UIGraphicsGetCurrentContext();
/* Unselected circle */
CGContextSaveGState(ctx);
{
CGContextAddPath(ctx, unselectedCircle.CGPath);
CGContextSetLineWidth(ctx, kStrokeWidth);
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(ctx, 229/255.0, 229/255.0, 229/255.0, 1.0);
CGContextDrawPath(ctx, kCGPathFillStroke);
}
CGContextRestoreGState(ctx);
/* Column separator */
CGContextSetRGBStrokeColor(ctx, 224/255.0, 224/255.0, 224/255.0, 1.0);
CGContextSetLineWidth(ctx, 1.0);
CGContextMoveToPoint(ctx, kColumnPosition, .0);
CGContextAddLineToPoint(ctx, kColumnPosition, self.bounds.size.height);
CGContextSetShouldAntialias(ctx, NO);
CGContextStrokePath(ctx);
[super drawRect:rect];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
label = [[UILabel alloc] initWithFrame:CGRectMake(kMarkCell, .0, self.frame.size.width - kMarkCell, self.frame.size.height)];
label.textColor = [UIColor blackColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:label];
imageView = [UIImageView new];
[self.contentView addSubview:imageView];
_renderedMark = [self renderMark];
}
return self;
}
#pragma mark - Properties
- (void)setIsSelected:(BOOL)isSelected
{
_isSelected = isSelected;
self.imageView.image = (isSelected) ? _renderedMark : nil;
}
- (UIImage *)renderMark
{
if(_renderedMark)
return _renderedMark;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(kMarkImageSize, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(kMarkImageSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath *markCircle = [UIBezierPath bezierPathWithOvalInRect:kCircleRect];
/* Background */
CGContextSaveGState(ctx);
{
CGContextAddPath(ctx, markCircle.CGPath);
CGContextSetFillColorWithColor(ctx, clearColorWithRGBHex(kMarkColor).CGColor);
CGContextSetShadowWithColor(ctx, kShadowOffset, kShadowRadius, kShadowColor.CGColor );
CGContextDrawPath(ctx, kCGPathFill);
}
CGContextRestoreGState(ctx);
/* Overlay */
CGContextSaveGState(ctx);
{
CGContextAddPath(ctx, markCircle.CGPath);
CGContextClip(ctx);
CGContextAddEllipseInRect(ctx, kCircleOverlayRect);
CGContextSetFillColorWithColor(ctx, colorWithRGBHex(kMarkColor).CGColor);
CGContextDrawPath(ctx, kCGPathFill);
}
CGContextRestoreGState(ctx);
/* Stroke */
CGContextSaveGState(ctx);
{
CGContextAddPath(ctx, markCircle.CGPath);
CGContextSetLineWidth(ctx, kStrokeWidth);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextDrawPath(ctx, kCGPathStroke);
}
CGContextRestoreGState(ctx);
/* Mark */
CGContextSaveGState(ctx);
{
CGContextSetShadowWithColor(ctx, kMarkShadowOffset, .0, kMarkShadowColor.CGColor );
CGContextMoveToPoint(ctx, kMarkBase.x, kMarkBase.y);
CGContextAddLineToPoint(ctx, kMarkBase.x + kMarkHeight * sin(kMarkDegrees), kMarkBase.y + kMarkHeight * cos(kMarkDegrees));
CGContextAddLineToPoint(ctx, kMarkDrawPoint.x, kMarkDrawPoint.y);
CGContextSetLineWidth(ctx, kMarkWidth);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextStrokePath(ctx);
}
CGContextRestoreGState(ctx);
UIImage *selectedMark = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return selectedMark;
}
@end