-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEnrichedTextTextView.mm
More file actions
71 lines (60 loc) · 2.44 KB
/
Copy pathEnrichedTextTextView.mm
File metadata and controls
71 lines (60 loc) · 2.44 KB
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
#import "EnrichedTextTextView.h"
#import "EnrichedTextView.h"
#import "htmlParser/HtmlParser.h"
#import "utils/EnrichedTextTouchHandler.h"
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
@implementation EnrichedTextTextView
- (void)copy:(id)sender {
EnrichedTextView *host = self.host;
if (host == nullptr) {
return;
}
// remove zero width spaces before copying the text
NSString *plainText = [host.textView.textStorage.string
substringWithRange:host.textView.selectedRange];
NSString *fixedPlainText =
[plainText stringByReplacingOccurrencesOfString:@"\u200B" withString:@""];
NSString *parsedHtml =
[HtmlParser parseToHtmlFromRange:host.textView.selectedRange host:host];
NSMutableAttributedString *attrStr = [[host.textView.textStorage
attributedSubstringFromRange:host.textView.selectedRange] mutableCopy];
NSRange fullAttrStrRange = NSMakeRange(0, attrStr.length);
[attrStr.mutableString replaceOccurrencesOfString:@"\u200B"
withString:@""
options:0
range:fullAttrStrRange];
NSData *rtfData =
[attrStr dataFromRange:NSMakeRange(0, attrStr.length)
documentAttributes:@{
NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType
}
error:nullptr];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setItems:@[ @{
UTTypeUTF8PlainText.identifier : fixedPlainText,
UTTypeHTML.identifier : parsedHtml,
UTTypeRTF.identifier : rtfData
} ]];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (touches.count == 1) {
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self];
[self.touchHandler handleTouchBeganAtPoint:point];
}
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (touches.count == 1) {
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self];
[self.touchHandler handleTouchEndedAtPoint:point];
}
[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches
withEvent:(UIEvent *)event {
[self.touchHandler handleTouchCancelled];
[super touchesCancelled:touches withEvent:event];
}
@end