Copy of rackspace-ios version 2.1.1
[pithos-ios] / Classes / LBTitleView.m
1 //
2 //  LBTitleView.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 6/20/11.
6 //  Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "LBTitleView.h"
10 #import "LoadBalancer.h"
11 #import <QuartzCore/QuartzCore.h>
12
13
14 @implementation LBTitleView
15
16 @synthesize loadBalancer, statusDot, nameLabel, connectedLabel, bwInLabel, bwOutLabel;
17
18 - (void)applyStyle {
19     self.frame = CGRectMake(0, 0, 320, 74);
20     self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
21     
22     // shadow
23     self.clipsToBounds = NO;
24     [self.layer setShadowColor:[[UIColor blackColor] CGColor]];
25     [self.layer setShadowRadius:2.0f];
26     [self.layer setShadowOffset:CGSizeMake(1, 1)];
27     [self.layer setShadowOpacity:0.8f];    
28     
29     // status dot
30     self.statusDot.frame = CGRectMake(10, 18, 13, 13);
31     self.statusDot.image = [self.loadBalancer imageForStatus];
32     //self.statusDot.image = [UIImage imageNamed:@"dot-green.png"];
33     [self addSubview:self.statusDot];
34     
35     // label styles
36     self.nameLabel.backgroundColor = [UIColor clearColor];
37     self.nameLabel.font = [UIFont systemFontOfSize:24];
38     self.nameLabel.adjustsFontSizeToFitWidth = YES;
39     
40     CGFloat third = self.frame.size.width / 3.0;
41     
42     self.connectedLabel.frame = CGRectMake(0, 48, third, 20);
43     self.connectedLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
44     self.connectedLabel.font = [UIFont systemFontOfSize:14];
45     self.connectedLabel.backgroundColor = [UIColor clearColor];
46     self.connectedLabel.textAlignment = UITextAlignmentCenter;
47     self.connectedLabel.text = @"";
48     self.connectedLabel.textColor = [UIColor darkGrayColor];
49     [self addSubview:self.connectedLabel];
50     
51     self.bwInLabel.frame = CGRectMake(third, 48, third, 20);
52     self.bwInLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
53     self.bwInLabel.font = [UIFont systemFontOfSize:14];
54     self.bwInLabel.backgroundColor = [UIColor clearColor];
55     self.bwInLabel.textAlignment = UITextAlignmentCenter;
56     self.bwInLabel.text = @"";
57     self.bwInLabel.textColor = [UIColor darkGrayColor];
58     [self addSubview:self.bwInLabel];
59     
60     self.bwOutLabel.frame = CGRectMake(third * 2, 48, third, 20);
61     self.bwOutLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
62     self.bwOutLabel.font = [UIFont systemFontOfSize:14];
63     self.bwOutLabel.backgroundColor = [UIColor clearColor];
64     self.bwOutLabel.textAlignment = UITextAlignmentCenter;
65     self.bwOutLabel.text = @"";
66     self.bwOutLabel.textColor = [UIColor darkGrayColor];
67     [self addSubview:self.bwOutLabel];
68     
69     // label positions
70     self.nameLabel.frame = CGRectMake(32, 9, 0, 0);
71     self.nameLabel.text = self.loadBalancer.name;
72     [self.nameLabel sizeToFit];
73     self.nameLabel.frame = CGRectMake(32, 9, MIN(self.frame.size.width - 40, self.nameLabel.frame.size.width), self.nameLabel.frame.size.height);
74     [self addSubview:self.nameLabel];
75     
76 }
77
78 - (id)init {
79     self = [super init];
80     if (self) {
81         self.statusDot = [[[UIImageView alloc] init] autorelease];
82         self.nameLabel = [[[UILabel alloc] init] autorelease];
83         self.connectedLabel = [[[UILabel alloc] init] autorelease];
84         self.bwInLabel = [[[UILabel alloc] init] autorelease];
85         self.bwOutLabel = [[[UILabel alloc] init] autorelease];
86     }
87     return self;
88 }
89
90 - (id)initWithLoadBalancer:(LoadBalancer *)lb {
91     self = [self init];
92     if (self) {
93         self.loadBalancer = lb;
94         [self applyStyle];
95     }
96     return self;
97 }
98
99 - (void)drawRect:(CGRect)rect {
100
101     CGContextRef context = UIGraphicsGetCurrentContext();
102
103     // draw gradient background
104     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
105     size_t num_locations = 2;
106     CGFloat locations[2] = { 1.0, 0.0 };
107     CGFloat components[8] = { 0.871, 0.871, 0.871, 1.0, 1.0, 1.0, 1.0, 1.0 };
108     CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
109     
110     CGContextSaveGState(context);
111     CGContextAddRect(context, rect);
112     CGContextClip(context);
113     CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, 74), 0);
114     CGContextRestoreGState(context);
115
116     CGColorSpaceRelease(colorspace);
117     CGGradientRelease(gradient);
118     
119     UIColor *lineColor = [UIColor colorWithWhite:0.784 alpha:1];
120
121     // draw etched lines
122     [lineColor set];
123     
124     // horizontal line
125         CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
126         CGContextSetLineWidth(context, 1.0);
127         CGContextMoveToPoint(context, 0.0, 43.0);
128         CGContextAddLineToPoint(context, rect.size.width, 43.0);
129         CGContextStrokePath(context);
130     
131     // vertical lines
132     float third = rect.size.width / 3.0;
133     
134     CGContextMoveToPoint(context, third, 43.0);
135         CGContextAddLineToPoint(context, third, rect.size.height);
136     CGContextStrokePath(context);
137     
138     CGContextMoveToPoint(context, third * 2, 43.0);
139         CGContextAddLineToPoint(context, third * 2, rect.size.height);
140     CGContextStrokePath(context);
141     
142     // size labels
143     self.connectedLabel.frame = CGRectMake(0, 48, third, 20);
144     self.bwInLabel.frame = CGRectMake(third, 48, third, 20);
145     self.bwOutLabel.frame = CGRectMake(third * 2, 48, third, 20);
146
147 }
148
149 - (void)dealloc {
150     [loadBalancer release];
151     [statusDot release];
152     [nameLabel release];
153     [connectedLabel release];
154     [bwInLabel release];
155     [bwOutLabel release];
156     [super dealloc];
157 }
158
159 @end