This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
TTStyledLabel.m
244 lines (196 loc) · 6.79 KB
/
TTStyledLabel.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#import "Three20/TTStyledLabel.h"
#import "Three20/TTStyledTextNode.h"
#import "Three20/TTStyledText.h"
#import "Three20/TTAppearance.h"
#import "Three20/TTNavigationCenter.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// global
static const CGFloat kCancelHighlightThreshold = 4;
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTStyledLabel
@synthesize text = _text, font = _font, textColor = _textColor, linkTextColor = _linkTextColor,
highlightedTextColor = _highlightedTextColor, highlighted = _highlighted,
highlightedNode = _highlightedNode;
///////////////////////////////////////////////////////////////////////////////////////////////////
// private
// UITableView looks for this function and crashes if it is not found when you select a cell
- (BOOL)isHighlighted {
return _highlighted;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_text = nil;
_font = nil;
_textColor = nil;
_highlightedTextColor = nil;
_linkTextColor = nil;
_highlighted = NO;
_highlightedNode = nil;
self.font = [UIFont systemFontOfSize:14];
self.textColor = [UIColor blackColor];
self.highlightedTextColor = [UIColor whiteColor];
self.linkTextColor = [TTAppearance appearance].linkTextColor;
self.backgroundColor = [UIColor whiteColor];
self.opaque = YES;
}
return self;
}
- (void)dealloc {
[_text release];
[_font release];
[_textColor release];
[_linkTextColor release];
[_highlightedTextColor release];
[_highlightedNode release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIView
- (void)drawRect:(CGRect)rect {
if (_highlighted) {
[_highlightedTextColor setFill];
} else {
[_textColor setFill];
}
[_text drawAtPoint:rect.origin highlighted:_highlighted];
}
- (void)layoutSubviews {
[super layoutSubviews];
_text.width = self.width;
}
- (CGSize)sizeThatFits:(CGSize)size {
_text.width = size.width;
return CGSizeMake(_text.width, _text.height);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIResponder
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
TTStyledTextFrame* frame = [_text hitTest:point];
if (frame && [frame.node isKindOfClass:[TTStyledLinkNode class]]) {
self.highlightedNode = (TTStyledLinkNode*)frame.node;
TTStyledTextTableView* tableView
= (TTStyledTextTableView*)[self firstParentOfClass:[TTStyledTextTableView class]];
if (tableView) {
tableView.highlightedLabel = self;
}
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (_highlightedNode) {
[[TTNavigationCenter defaultCenter] displayURL:_highlightedNode.url];
self.highlightedNode = nil;
TTStyledTextTableView* tableView
= (TTStyledTextTableView*)[self firstParentOfClass:[TTStyledTextTableView class]];
if (tableView) {
tableView.highlightedLabel = nil;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// public
- (void)setText:(TTStyledText*)text {
if (text != _text) {
[_text release];
_text = [text retain];
_text.font = _font;
[self setNeedsDisplay];
}
}
- (void)setFont:(UIFont*)font {
if (font != _font) {
[_font release];
_font = [font retain];
_text.font = _font;
[self setNeedsDisplay];
}
}
- (void)setTextColor:(UIColor*)textColor {
if (textColor != _textColor) {
[_textColor release];
_textColor = [textColor retain];
[self setNeedsDisplay];
}
}
- (void)setHighlightedNode:(TTStyledLinkNode*)highlightedNode {
if (highlightedNode != _highlightedNode) {
_highlightedNode.highlighted = NO;
[_highlightedNode release];
_highlightedNode = [highlightedNode retain];
_highlightedNode.highlighted = YES;
[self setNeedsDisplay];
}
}
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTStyledTextTableView
@synthesize highlightedLabel = _highlightedLabel;
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if (self = [super initWithFrame:frame style:style]) {
_highlightedLabel = nil;
_highlightStartPoint = CGPointZero;
_highlightTimer = nil;
self.delaysContentTouches = NO;
}
return self;
}
- (void)dealloc {
[_highlightedLabel release];
[_highlightTimer invalidate];
[super dealloc];
}
- (void)delayedTouchesEnded:(NSTimer*)timer {
_highlightTimer = nil;
self.highlightedLabel = nil;
NSString* url = timer.userInfo;
[[TTNavigationCenter defaultCenter] displayURL:url];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIResponder
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
[_highlightTimer invalidate];
_highlightTimer = nil;
if (_highlightedLabel) {
UITouch* touch = [touches anyObject];
_highlightStartPoint = [touch locationInView:self];
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesMoved:touches withEvent:event];
if (_highlightedLabel) {
UITouch* touch = [touches anyObject];
CGPoint newPoint = [touch locationInView:self];
CGFloat dx = newPoint.x - _highlightStartPoint.x;
CGFloat dy = newPoint.y - _highlightStartPoint.y;
CGFloat d = sqrt((dx*dx) + (dy+dy));
if (d > kCancelHighlightThreshold) {
_highlightedLabel.highlightedNode = nil;
self.highlightedLabel = nil;
}
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (_highlightedLabel) {
NSString* url = _highlightedLabel.highlightedNode.url;
_highlightedLabel.highlightedNode = nil;
_highlightTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self
selector:@selector(delayedTouchesEnded:) userInfo:url repeats:NO];
} else {
[super touchesEnded:touches withEvent:event];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UITableView
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
scrollPosition:(UITableViewScrollPosition)scrollPosition {
if (!_highlightedLabel) {
[super selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
}
}
@end