forked from facebook/componentkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCKTextComponentViewControlTracker.mm
93 lines (82 loc) · 3.39 KB
/
CKTextComponentViewControlTracker.mm
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
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "CKTextComponentViewControlTracker.h"
#import <ComponentKit/CKTextKitRenderer+Positioning.h>
#import <ComponentKit/CKTextKitRenderer+TextChecking.h>
#import "CKTextComponentLayer.h"
#import "CKTextComponentLayerHighlighter.h"
#import "CKTextComponentViewInternal.h"
@implementation CKTextComponentViewControlTracker
{
NSTextCheckingResult *_trackingTextCheckingResult;
}
/**
sendActionsForControlEvents: calls sendAction:to:forEvent: with a nil event, so provide this alternate method to supply
the event to the targets
*/
- (void)_sendActionsToControl:(UIControl *)control forControlEvents:(UIControlEvents)controlEvents withEvent:(UIEvent *)event
{
for (id target in control.allTargets) {
id realTarget = (target == [NSNull null]) ? nil : target;
for (NSString *actionName in [control actionsForTarget:realTarget forControlEvent:controlEvents]) {
[control sendAction:NSSelectorFromString(actionName) to:realTarget forEvent:event];
}
}
}
- (BOOL)beginTrackingForTextComponentView:(CKTextComponentView *)view
withTouch:(UITouch *)touch
withEvent:(UIEvent *)event
{
CGPoint point = [touch locationInView:view];
NSTextCheckingResult *trackingTextCheckingResult = [view.renderer textCheckingResultAtPoint:point];
if (trackingTextCheckingResult != nil) {
view.textLayer.highlighter.highlightedRange = trackingTextCheckingResult.range;
[self _sendActionsToControl:view forControlEvents:CKUIControlEventTextViewDidBeginHighlightingText withEvent:event];
_trackingTextCheckingResult = trackingTextCheckingResult;
}
return YES;
}
- (BOOL)continueTrackingForTextComponentView:(CKTextComponentView *)view
withTouch:(UITouch *)touch
withEvent:(UIEvent *)event
{
if (_trackingTextCheckingResult) {
CGPoint point = [touch locationInView:view];
NSUInteger index = [view.renderer nearestTextIndexAtPosition:point];
NSRange range = _trackingTextCheckingResult.range;
if (!NSLocationInRange(index, range)) {
[view cancelTrackingWithEvent:event];
return NO;
}
}
return YES;
}
- (void)endTrackingForTextComponentView:(CKTextComponentView *)view
withTouch:(UITouch *)touch
withEvent:(UIEvent *)event
{
if (_trackingTextCheckingResult != nil) {
_trackingTextCheckingResult = nil;
if (touch != nil) {
[self _sendActionsToControl:view forControlEvents:CKUIControlEventTextViewDidEndHighlightingText withEvent:event];
}
view.textLayer.highlighter.highlightedRange = CKTextComponentLayerInvalidHighlightRange;
}
}
- (void)cancelTrackingForTextComponentView:(CKTextComponentView *)view
withEvent:(UIEvent *)event
{
if (_trackingTextCheckingResult != nil) {
_trackingTextCheckingResult = nil;
[self _sendActionsToControl:view forControlEvents:CKUIControlEventTextViewDidCancelHighlightingText withEvent:event];
view.textLayer.highlighter.highlightedRange = CKTextComponentLayerInvalidHighlightRange;
}
}
@end