forked from camh/CHGridView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHSectionHeaderView.m
118 lines (88 loc) · 3.49 KB
/
CHSectionHeaderView.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
//
// CHSectionTitle.m
//
// RELEASED UNDER THE MIT LICENSE
//
// Created by Cameron Kenly Hunt on 2/18/10.
// Copyright 2010 Cameron Kenley Hunt All rights reserved.
// http://cameron.io/project/chgridview
//
#import "CHSectionHeaderView.h"
@implementation CHSectionHeaderView
@synthesize section, title, yCoordinate;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
if(title == nil)
title = [[NSString alloc] initWithString:@""];
if(topLine == nil)
topLine = [[UIView alloc] init];
[topLine setBackgroundColor:[UIColor colorWithHue:0.56f saturation:0.15f brightness:0.52f alpha:1.0f]];
[topLine setOpaque:YES];
[self addSubview:topLine];
[self setOpaque:YES];
[self setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:1.0f]];
[self setContentMode:UIViewContentModeRedraw];
}
return self;
}
- (void)dealloc {
[topLine release];
[title release];
[super dealloc];
}
- (void)setSection:(int)s{
section = s;
if([title isEqualToString:@""]){
[self setTitle:[NSString stringWithFormat:@"Section %i", section]];
}
}
- (void)setOpaque:(BOOL)b{
[super setOpaque:b];
if(b) [self setBackgroundColor:[UIColor whiteColor]];
else [self setBackgroundColor:[UIColor clearColor]];
}
- (void)layoutSubviews{
CGRect b = self.bounds;
[topLine setFrame:CGRectMake(b.origin.x, b.origin.y - 1.0f, b.size.width, 1.0f)];
}
- (void)drawRect:(CGRect)rect {
//subclasses should implement drawRect: but must draw title on their own
CGContextRef c = UIGraphicsGetCurrentContext();
CGRect b = self.bounds;
float padding = 10.0f;
float fontHeight = floorf(b.size.height * 0.60f);
if(fontHeight > 20.0f) fontHeight = 20.0f;
//graw gradient
CGGradientRef tabGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
float gradientOpacity = 0.85f;
CGFloat locations[2] = { 0.0f, 1.0f };
// RGBA values for start and end colors
CGFloat components[8] = { 0.572f, 0.627f, 0.670f, gradientOpacity, // Start color
0.721f, 0.756f, 0.784f, gradientOpacity }; // End color
rgbColorspace = CGColorSpaceCreateDeviceRGB();
tabGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGRect currentBounds = self.bounds;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), currentBounds.size.height);
CGContextDrawLinearGradient(c, tabGradient, topCenter, bottomCenter, 0);
CGGradientRelease(tabGradient);
CGColorSpaceRelease(rgbColorspace);
//draw lines
[[UIColor colorWithWhite:1.0f alpha:0.2f] set];
CGContextFillRect(c, CGRectMake(b.origin.x, b.origin.y, b.size.width, 1.0f));
[[UIColor colorWithHue:0.42f saturation:0.07f brightness:0.64f alpha:1.0f] set];
CGContextFillRect(c, CGRectMake(b.origin.x, b.size.height - 1.0f, b.size.width, 1.0f));
//draw title
float textWidth = b.size.width - (padding * 2.0f);
UIFont *f = [UIFont boldSystemFontOfSize:fontHeight];
[[UIColor whiteColor] set];
CGContextSetShadow(c, CGSizeMake(0, -1.0f), 1.0f);
CGSize fontSize = [title sizeWithFont:f forWidth:textWidth lineBreakMode:UILineBreakModeTailTruncation];
[title drawInRect:CGRectMake(padding, ceilf((b.size.height - fontSize.height) / 2), textWidth, fontSize.height) withFont:f lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
}
- (NSString *)description{
return [NSString stringWithFormat:@"%@ section index = %i", [super description], section];
}
@end