forked from facebook/componentkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCKTextComponentView.mm
138 lines (118 loc) · 4.13 KB
/
CKTextComponentView.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
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
/*
* 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 "CKTextComponentView.h"
#import "CKTextComponentViewInternal.h"
#import <ComponentKit/CKAssert.h>
#import <ComponentKit/CKAsyncLayer.h>
#import <ComponentKit/CKAsyncLayerSubclass.h>
#import <ComponentKit/CKTextKitRenderer.h>
#import <ComponentKit/CKTextKitRendererCache.h>
#import <ComponentKit/CKInternalHelpers.h>
#import "CKTextComponentLayer.h"
#import "CKTextComponentLayerHighlighter.h"
#import "CKTextComponentViewControlTracker.h"
@implementation CKTextComponentView
{
CKTextComponentViewControlTracker *_controlTracker;
}
+ (Class)layerClass
{
return [CKTextComponentLayer class];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// Set some sensible defaults for a text view
self.contentScaleFactor = CKScreenScale();
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
if (![self.backgroundColor isEqual:backgroundColor]) {
BOOL opaque = self.textLayer.opaque;
[super setBackgroundColor:backgroundColor];
// for reasons I don't understand, UIView is setting opaque=NO on self.layer when setting the background color. we
// don't want to force our rich text layers to draw with blending, so check if we can keep the opacity value after
// setting the backgroundColor.
if (opaque) {
CGFloat alpha = 0.0;
if ([backgroundColor getRed:NULL green:NULL blue:NULL alpha:&alpha] ||
[backgroundColor getWhite:NULL alpha:&alpha] ||
[backgroundColor getHue:NULL saturation:NULL brightness:NULL alpha:&alpha]) {
if (alpha == 1.0) {
self.textLayer.opaque = YES;
[self.textLayer setNeedsDisplay];
}
}
}
}
}
- (CKTextComponentLayer *)textLayer
{
return (CKTextComponentLayer *)self.layer;
}
- (void)setRenderer:(CKTextKitRenderer *)renderer
{
[self.textLayer setRenderer:renderer];
}
- (CKTextKitRenderer *)renderer
{
return [self.textLayer renderer];
}
#pragma mark - Control Tracking
- (CKTextComponentViewControlTracker *)controlTracker
{
if (!_controlTracker) {
// Lazily generate a control tracker to receive UIControl touch input.
_controlTracker = [[CKTextComponentViewControlTracker alloc] init];
}
return _controlTracker;
}
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
if (![super beginTrackingWithTouch:touch withEvent:event]) {
return NO;
}
return [self.controlTracker beginTrackingForTextComponentView:self withTouch:touch withEvent:event];
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
if (![super continueTrackingWithTouch:touch withEvent:event]) {
return NO;
}
return [self.controlTracker continueTrackingForTextComponentView:self withTouch:touch withEvent:event];
}
- (void)cancelTrackingWithEvent:(UIEvent *)event
{
[self.controlTracker cancelTrackingForTextComponentView:self withEvent:event];
[super cancelTrackingWithEvent:event];
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
[self.controlTracker endTrackingForTextComponentView:self withTouch:touch withEvent:event];
[super endTrackingWithTouch:touch withEvent:event];
}
#pragma mark - Touch Interaction
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)recognizer
{
// We want the same behavior as UIButton: "...a UIButton, by default, does return NO for a single tap
// UITapGestureRecognizer whose view is not the UIButton itself."
// http://www.apeth.com/iOSBook/ch18.html#_gesture_recognizers
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer *)recognizer;
if (tapRecognizer.numberOfTapsRequired == 1 && tapRecognizer.view != self) {
return NO;
}
}
return [super gestureRecognizerShouldBegin:recognizer];
}
@end