forked from facebookarchive/three20
/
TTSearchlightLabel.m
145 lines (118 loc) · 4.38 KB
/
TTSearchlightLabel.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
#import "Three20/TTSearchlightLabel.h"
#import "Three20/TTDefaultStyleSheet.h"
@implementation TTSearchlightLabel
@synthesize text = _text, font = _font, textColor, spotlightColor = _spotlightColor,
textAlignment = _textAlignment;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_timer = nil;
self.text = @"";
self.font = TTSTYLEVAR(font);
self.textColor = [UIColor colorWithWhite:0.25 alpha:1];
self.spotlightColor = [UIColor whiteColor];
self.textAlignment = UITextAlignmentLeft;
self.backgroundColor = [UIColor clearColor];
self.contentMode = UIViewContentModeCenter;
}
return self;
}
- (void)dealloc {
[self stopAnimating];
[_text release];
[_font release];
[textColor release];
[_spotlightColor release];
[super dealloc];
}
//////////////////////////////////////////////////////////////////////////////////////////////////
- (void)createMask {
CGRect rect = self.frame;
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
int bitmapBytesPerRow = (rect.size.width * 4);
int bitmapByteCount = (bitmapBytesPerRow * rect.size.height);
_maskData = malloc(bitmapByteCount);
_maskContext = CGBitmapContextCreate(_maskData, rect.size.width, rect.size.height,
8, bitmapBytesPerRow, space, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
}
- (void)releaseMask {
CGContextRelease(_maskContext);
free(_maskData);
_maskContext = nil;
_maskData = nil;
}
- (CGImageRef)createSpotlightMask:(CGRect)rect origin:(CGPoint)origin radius:(CGFloat)radius {
CGContextClearRect(_maskContext, rect);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {1, 1, 1, 1, 0, 0, 0, 0};
CGFloat locations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components, locations, 2);
CGContextDrawRadialGradient(_maskContext, gradient, origin, 0, origin, radius, 0);
CGGradientRelease(gradient);
CGColorSpaceRelease(space);
return CGBitmapContextCreateImage(_maskContext);
}
- (void)updateSpotlight {
_spotlightPoint += 1.3/32;
if (_spotlightPoint > 2) {
_spotlightPoint = -0.5;
}
if (_spotlightPoint <= 1.5) {
[self setNeedsDisplay];
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// UIView
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGSize textSize = [self sizeThatFits:CGSizeZero];
CGFloat x = 0;
if (_textAlignment == UITextAlignmentRight) {
x = self.frame.size.width - textSize.width;
} else if (_textAlignment == UITextAlignmentCenter) {
x = ceil(self.frame.size.width/2 - textSize.width/2);
}
CGFloat y = 0;
if (self.contentMode == UIViewContentModeCenter) {
y = ceil(rect.size.height/2 + _font.capHeight/2);
} else if (self.contentMode == UIViewContentModeBottom) {
y = rect.size.height + _font.descender;
} else {
y = _font.capHeight;
}
CGContextSelectFont(context, [_font.fontName UTF8String], _font.pointSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextMatrix(context, CGAffineTransformScale(CGAffineTransformIdentity, 1, -1));
CGContextSetFillColorWithColor(context, textColor.CGColor);
CGContextShowTextAtPoint(context, x, y, [self.text UTF8String], self.text.length);
if (_timer) {
CGPoint spotOrigin = CGPointMake(x + (textSize.width * _spotlightPoint),
y - ceil(self.font.capHeight/2));
CGFloat spotRadius = self.font.capHeight*2;
CGImageRef mask = [self createSpotlightMask:rect origin:spotOrigin radius:spotRadius];
CGContextClipToMask(context, rect, mask);
CGImageRelease(mask);
[_spotlightColor setFill];
CGContextShowTextAtPoint(context, x, y, [self.text UTF8String], self.text.length);
}
}
- (CGSize)sizeThatFits:(CGSize)size {
return [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
}
//////////////////////////////////////////////////////////////////////////////////////////////////
- (void)startAnimating {
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0/32 target:self
selector:@selector(updateSpotlight) userInfo:nil repeats:YES];
_spotlightPoint = -0.5;
[self createMask];
}
}
- (void)stopAnimating {
if (_timer) {
[_timer invalidate];
_timer = nil;
[self releaseMask];
}
}
@end