Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7640 views
1
#include "common.h"
2
#import "MuTextSelectView.h"
3
#import "MuWord.h"
4
5
@implementation MuTextSelectView
6
{
7
NSArray *words;
8
CGSize pageSize;
9
UIColor *color;
10
CGPoint start;
11
CGPoint end;
12
}
13
14
- (id) initWithWords:(NSArray *)_words pageSize:(CGSize)_pageSize
15
{
16
self = [super initWithFrame:CGRectMake(0,0,100,100)];
17
if (self)
18
{
19
[self setOpaque:NO];
20
words = [_words retain];
21
pageSize = _pageSize;
22
color = [[UIColor colorWithRed:0x25/255.0 green:0x72/255.0 blue:0xAC/255.0 alpha:0.5] retain];
23
UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onDrag:)];
24
[self addGestureRecognizer:rec];
25
[rec release];
26
}
27
return self;
28
}
29
30
-(void) dealloc
31
{
32
[words release];
33
[color release];
34
[super dealloc];
35
}
36
37
- (NSArray *) selectionRects
38
{
39
NSMutableArray *arr = [NSMutableArray array];
40
__block CGRect r;
41
42
[MuWord selectFrom:start to:end fromWords:words
43
onStartLine:^{
44
r = CGRectNull;
45
} onWord:^(MuWord *w) {
46
r = CGRectUnion(r, w.rect);
47
} onEndLine:^{
48
if (!CGRectIsNull(r))
49
[arr addObject:[NSValue valueWithCGRect:r]];
50
}];
51
52
return arr;
53
}
54
55
- (NSString *) selectedText
56
{
57
__block NSMutableString *text = [NSMutableString string];
58
__block NSMutableString *line;
59
60
[MuWord selectFrom:start to:end fromWords:words
61
onStartLine:^{
62
line = [NSMutableString string];
63
} onWord:^(MuWord *w) {
64
if (line.length > 0)
65
[line appendString:@" "];
66
[line appendString:w.string];
67
} onEndLine:^{
68
if (text.length > 0)
69
[text appendString:@"\n"];
70
[text appendString:line];
71
}];
72
73
return text;
74
}
75
76
-(void) onDrag:(UIPanGestureRecognizer *)rec
77
{
78
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
79
CGPoint p = [rec locationInView:self];
80
p.x /= scale.width;
81
p.y /= scale.height;
82
83
if (rec.state == UIGestureRecognizerStateBegan)
84
start = p;
85
86
end = p;
87
88
[self setNeedsDisplay];
89
}
90
91
- (void) drawRect:(CGRect)rect
92
{
93
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
94
CGContextRef cref = UIGraphicsGetCurrentContext();
95
CGContextScaleCTM(cref, scale.width, scale.height);
96
__block CGRect r;
97
98
[color set];
99
100
[MuWord selectFrom:start to:end fromWords:words
101
onStartLine:^{
102
r = CGRectNull;
103
} onWord:^(MuWord *w) {
104
r = CGRectUnion(r, w.rect);
105
} onEndLine:^{
106
if (!CGRectIsNull(r))
107
UIRectFill(r);
108
}];
109
}
110
111
@end
112
113