blob: eac3132ac935832561396a9962458292c8acf64f (
plain)
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
|
#include "common.h"
#import "MuTextSelectView.h"
#import "MuWord.h"
@implementation MuTextSelectView
- (id) initWithWords:(NSArray *)_words pageSize:(CGSize)_pageSize
{
self = [super initWithFrame:CGRectMake(0,0,100,100)];
if (self)
{
[self setOpaque:NO];
words = [_words retain];
pageSize = _pageSize;
color = [[UIColor colorWithRed:0x25/255.0 green:0x72/255.0 blue:0xAC/255.0 alpha:0.5] retain];
UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onDrag:)];
[self addGestureRecognizer:rec];
[rec release];
}
return self;
}
-(void) dealloc
{
[words release];
[color release];
[super dealloc];
}
- (NSArray *) selectionRects
{
NSMutableArray *arr = [NSMutableArray array];
__block CGRect r;
[MuWord selectFrom:start to:end fromWords:words
onStartLine:^{
r = CGRectNull;
} onWord:^(MuWord *w) {
r = CGRectUnion(r, w.rect);
} onEndLine:^{
if (!CGRectIsNull(r))
[arr addObject:[NSValue valueWithCGRect:r]];
}];
return arr;
}
- (NSString *) selectedText
{
__block NSMutableString *text = [NSMutableString string];
__block NSMutableString *line;
[MuWord selectFrom:start to:end fromWords:words
onStartLine:^{
line = [NSMutableString string];
} onWord:^(MuWord *w) {
if (line.length > 0)
[line appendString:@" "];
[line appendString:w.string];
} onEndLine:^{
if (text.length > 0)
[text appendString:@"\n"];
[text appendString:line];
}];
return text;
}
-(void) onDrag:(UIPanGestureRecognizer *)rec
{
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
CGPoint p = [rec locationInView:self];
p.x /= scale.width;
p.y /= scale.height;
if (rec.state == UIGestureRecognizerStateBegan)
start = p;
end = p;
[self setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect
{
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
CGContextRef cref = UIGraphicsGetCurrentContext();
CGContextScaleCTM(cref, scale.width, scale.height);
__block CGRect r;
[color set];
[MuWord selectFrom:start to:end fromWords:words
onStartLine:^{
r = CGRectNull;
} onWord:^(MuWord *w) {
r = CGRectUnion(r, w.rect);
} onEndLine:^{
if (!CGRectIsNull(r))
UIRectFill(r);
}];
}
@end
|