forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASEditableTextNode.mm
853 lines (700 loc) · 30.3 KB
/
ASEditableTextNode.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//
// ASEditableTextNode.mm
// Texture
//
// 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 /ASDK-Licenses directory of this source tree. An additional
// grant of patent rights can be found in the PATENTS file in the same directory.
//
// Modifications to this file made after 4/13/2017 are: Copyright (c) 2017-present,
// Pinterest, Inc. Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASEditableTextNode.h>
#import <objc/message.h>
#import <tgmath.h>
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h>
#import <AsyncDisplayKit/ASEqualityHelpers.h>
#import <AsyncDisplayKit/ASTextKitComponents.h>
#import <AsyncDisplayKit/ASTextNodeWordKerner.h>
#import <AsyncDisplayKit/ASThread.h>
/**
@abstract Object to hold UITextView's pending UITextInputTraits
**/
@interface _ASTextInputTraitsPendingState : NSObject
@property (nonatomic, readwrite, assign) UITextAutocapitalizationType autocapitalizationType;
@property (nonatomic, readwrite, assign) UITextAutocorrectionType autocorrectionType;
@property (nonatomic, readwrite, assign) UITextSpellCheckingType spellCheckingType;
@property (nonatomic, readwrite, assign) UIKeyboardAppearance keyboardAppearance;
@property (nonatomic, readwrite, assign) UIKeyboardType keyboardType;
@property (nonatomic, readwrite, assign) UIReturnKeyType returnKeyType;
@property (nonatomic, readwrite, assign) BOOL enablesReturnKeyAutomatically;
@property (nonatomic, readwrite, assign, getter=isSecureTextEntry) BOOL secureTextEntry;
@end
@implementation _ASTextInputTraitsPendingState
- (instancetype)init
{
if (!(self = [super init]))
return nil;
// set default values, as defined in Apple's comments in UITextInputTraits.h
_autocapitalizationType = UITextAutocapitalizationTypeSentences;
_autocorrectionType = UITextAutocorrectionTypeDefault;
_spellCheckingType = UITextSpellCheckingTypeDefault;
_keyboardAppearance = UIKeyboardAppearanceDefault;
_keyboardType = UIKeyboardTypeDefault;
_returnKeyType = UIReturnKeyDefault;
return self;
}
@end
/**
@abstract As originally reported in rdar://14729288, when scrollEnabled = NO,
UITextView does not calculate its contentSize. This makes it difficult
for a client to embed a UITextView inside a different scroll view with
other content (setting scrollEnabled = NO on the UITextView itself,
because the containing scroll view will handle the gesture)...
because accessing contentSize is typically necessary to perform layout.
Apple later closed the issue as expected behavior. This works around
the issue by ensuring that contentSize is always calculated, while
still providing control over the UITextView's scrolling.
See issue: https://github.com/facebook/AsyncDisplayKit/issues/1063
*/
@interface ASPanningOverriddenUITextView : ASTextKitComponentsTextView
{
BOOL _shouldBlockPanGesture;
}
@end
@implementation ASPanningOverriddenUITextView
#if TARGET_OS_IOS
// tvOS doesn't support self.scrollsToTop
- (BOOL)scrollEnabled
{
return _shouldBlockPanGesture;
}
- (void)setScrollEnabled:(BOOL)scrollEnabled
{
_shouldBlockPanGesture = !scrollEnabled;
self.scrollsToTop = scrollEnabled;
[super setScrollEnabled:YES];
}
#endif
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// Never allow our pans to begin when _shouldBlockPanGesture is true.
if (_shouldBlockPanGesture && gestureRecognizer == self.panGestureRecognizer)
return NO;
// Otherwise, proceed as usual.
if ([UITextView instancesRespondToSelector:_cmd])
return [super gestureRecognizerShouldBegin:gestureRecognizer];
return YES;
}
@end
#pragma mark -
@interface ASEditableTextNode () <UITextViewDelegate, NSLayoutManagerDelegate>
{
@private
// Configuration.
NSDictionary *_typingAttributes;
// Core.
id <ASEditableTextNodeDelegate> __weak _delegate;
BOOL _delegateDidUpdateEnqueued;
// TextKit.
ASDN::RecursiveMutex _textKitLock;
ASTextKitComponents *_textKitComponents;
ASTextKitComponents *_placeholderTextKitComponents;
// Forwards NSLayoutManagerDelegate methods related to word kerning
ASTextNodeWordKerner *_wordKerner;
// UITextInputTraits
ASDN::RecursiveMutex _textInputTraitsLock;
_ASTextInputTraitsPendingState *_textInputTraits;
// Misc. State.
BOOL _displayingPlaceholder; // Defaults to YES.
BOOL _isPreservingSelection;
BOOL _selectionChangedForEditedText;
NSRange _previousSelectedRange;
}
@property (nonatomic, strong, readonly) _ASTextInputTraitsPendingState *textInputTraits;
@end
@implementation ASEditableTextNode
#pragma mark - NSObject Overrides
- (instancetype)init
{
return [self initWithTextKitComponents:[ASTextKitComponents componentsWithAttributedSeedString:nil textContainerSize:CGSizeZero]
placeholderTextKitComponents:[ASTextKitComponents componentsWithAttributedSeedString:nil textContainerSize:CGSizeZero]];
}
- (instancetype)initWithTextKitComponents:(ASTextKitComponents *)textKitComponents
placeholderTextKitComponents:(ASTextKitComponents *)placeholderTextKitComponents
{
if (!(self = [super init]))
return nil;
_displayingPlaceholder = YES;
_scrollEnabled = YES;
// Create the scaffolding for the text view.
_textKitComponents = textKitComponents;
_textKitComponents.layoutManager.delegate = self;
_wordKerner = [[ASTextNodeWordKerner alloc] init];
_textContainerInset = UIEdgeInsetsZero;
// Create the placeholder scaffolding.
_placeholderTextKitComponents = placeholderTextKitComponents;
_placeholderTextKitComponents.layoutManager.delegate = self;
return self;
}
#pragma mark - ASDisplayNode Overrides
- (void)didLoad
{
[super didLoad];
void (^configureTextView)(UITextView *) = ^(UITextView *textView) {
if (!_displayingPlaceholder || textView != _textKitComponents.textView) {
// If showing the placeholder, don't propagate backgroundColor/opaque to the editable textView. It is positioned over the placeholder to accept taps to begin editing, and if it's opaque/colored then it'll obscure the placeholder.
textView.backgroundColor = self.backgroundColor;
textView.opaque = self.opaque;
} else if (_displayingPlaceholder && textView == _textKitComponents.textView) {
// The default backgroundColor for a textView is white. Due to the reason described above, make sure the editable textView starts out transparent.
textView.backgroundColor = nil;
textView.opaque = NO;
}
textView.textContainerInset = self.textContainerInset;
// Configure textView with UITextInputTraits
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (_textInputTraits) {
textView.autocapitalizationType = _textInputTraits.autocapitalizationType;
textView.autocorrectionType = _textInputTraits.autocorrectionType;
textView.spellCheckingType = _textInputTraits.spellCheckingType;
textView.keyboardType = _textInputTraits.keyboardType;
textView.keyboardAppearance = _textInputTraits.keyboardAppearance;
textView.returnKeyType = _textInputTraits.returnKeyType;
textView.enablesReturnKeyAutomatically = _textInputTraits.enablesReturnKeyAutomatically;
textView.secureTextEntry = _textInputTraits.isSecureTextEntry;
}
}
[self.view addSubview:textView];
};
ASDN::MutexLocker l(_textKitLock);
// Create and configure the placeholder text view.
_placeholderTextKitComponents.textView = [[ASTextKitComponentsTextView alloc] initWithFrame:CGRectZero textContainer:_placeholderTextKitComponents.textContainer];
_placeholderTextKitComponents.textView.userInteractionEnabled = NO;
_placeholderTextKitComponents.textView.accessibilityElementsHidden = YES;
configureTextView(_placeholderTextKitComponents.textView);
// Create and configure our text view.
_textKitComponents.textView = [[ASPanningOverriddenUITextView alloc] initWithFrame:CGRectZero textContainer:_textKitComponents.textContainer];
_textKitComponents.textView.scrollEnabled = _scrollEnabled;
_textKitComponents.textView.delegate = self;
#if TARGET_OS_IOS
_textKitComponents.textView.editable = YES;
#endif
_textKitComponents.textView.typingAttributes = _typingAttributes;
_textKitComponents.textView.accessibilityHint = _placeholderTextKitComponents.textStorage.string;
configureTextView(_textKitComponents.textView);
[self _updateDisplayingPlaceholder];
// once view is loaded, setters set directly on view
_textInputTraits = nil;
}
- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
{
ASTextKitComponents *displayedComponents = [self isDisplayingPlaceholder] ? _placeholderTextKitComponents : _textKitComponents;
CGSize textSize;
if (_maximumLinesToDisplay > 0) {
textSize = [displayedComponents sizeForConstrainedWidth:constrainedSize.width
forMaxNumberOfLines: _maximumLinesToDisplay];
} else {
textSize = [displayedComponents sizeForConstrainedWidth:constrainedSize.width];
}
CGFloat width = std::ceil(textSize.width + _textContainerInset.left + _textContainerInset.right);
CGFloat height = std::ceil(textSize.height + _textContainerInset.top + _textContainerInset.bottom);
return CGSizeMake(std::fmin(width, constrainedSize.width), std::fmin(height, constrainedSize.height));
}
- (void)layout
{
ASDisplayNodeAssertMainThread();
[super layout];
[self _layoutTextView];
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
ASDN::MutexLocker l(_textKitLock);
// If showing the placeholder, don't propagate backgroundColor/opaque to the editable textView. It is positioned over the placeholder to accept taps to begin editing, and if it's opaque/colored then it'll obscure the placeholder.
// The backgroundColor/opaque will be propagated to the editable textView when editing begins.
if (!_displayingPlaceholder) {
_textKitComponents.textView.backgroundColor = backgroundColor;
}
_placeholderTextKitComponents.textView.backgroundColor = backgroundColor;
}
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
{
ASDN::MutexLocker l(_textKitLock);
_textContainerInset = textContainerInset;
_textKitComponents.textView.textContainerInset = textContainerInset;
_placeholderTextKitComponents.textView.textContainerInset = textContainerInset;
}
- (void)setOpaque:(BOOL)opaque
{
[super setOpaque:opaque];
ASDN::MutexLocker l(_textKitLock);
// If showing the placeholder, don't propagate backgroundColor/opaque to the editable textView. It is positioned over the placeholder to accept taps to begin editing, and if it's opaque/colored then it'll obscure the placeholder.
// The backgroundColor/opaque will be propagated to the editable textView when editing begins.
if (!_displayingPlaceholder) {
_textKitComponents.textView.opaque = opaque;
}
_placeholderTextKitComponents.textView.opaque = opaque;
}
- (void)setLayerBacked:(BOOL)layerBacked
{
ASDisplayNodeAssert(!layerBacked, @"Cannot set layerBacked to YES on ASEditableTextNode – instances must be view-backed in order to ensure touch events can be passed to the internal UITextView during editing.");
[super setLayerBacked:layerBacked];
}
- (BOOL)supportsLayerBacking
{
return NO;
}
#pragma mark - Configuration
@synthesize delegate = _delegate;
- (void)setScrollEnabled:(BOOL)scrollEnabled
{
ASDN::MutexLocker l(_textKitLock);
_scrollEnabled = scrollEnabled;
[_textKitComponents.textView setScrollEnabled:_scrollEnabled];
}
- (UITextView *)textView
{
ASDisplayNodeAssertMainThread();
[self view];
ASDisplayNodeAssert(_textKitComponents.textView != nil, @"UITextView must be created in -[ASEditableTextNode didLoad]");
return _textKitComponents.textView;
}
- (void)setMaximumLinesToDisplay:(NSUInteger)maximumLines
{
_maximumLinesToDisplay = maximumLines;
[self setNeedsLayout];
}
#pragma mark -
@dynamic typingAttributes;
- (NSDictionary *)typingAttributes
{
return _typingAttributes;
}
- (void)setTypingAttributes:(NSDictionary *)typingAttributes
{
if (ASObjectIsEqual(typingAttributes, _typingAttributes))
return;
_typingAttributes = [typingAttributes copy];
ASDN::MutexLocker l(_textKitLock);
_textKitComponents.textView.typingAttributes = _typingAttributes;
}
#pragma mark -
@dynamic selectedRange;
- (NSRange)selectedRange
{
ASDN::MutexLocker l(_textKitLock);
return _textKitComponents.textView.selectedRange;
}
- (void)setSelectedRange:(NSRange)selectedRange
{
ASDN::MutexLocker l(_textKitLock);
_textKitComponents.textView.selectedRange = selectedRange;
}
#pragma mark - Placeholder
- (BOOL)isDisplayingPlaceholder
{
return _displayingPlaceholder;
}
#pragma mark -
@dynamic attributedPlaceholderText;
- (NSAttributedString *)attributedPlaceholderText
{
ASDN::MutexLocker l(_textKitLock);
return [_placeholderTextKitComponents.textStorage copy];
}
- (void)setAttributedPlaceholderText:(NSAttributedString *)attributedPlaceholderText
{
ASDN::MutexLocker l(_textKitLock);
if (ASObjectIsEqual(_placeholderTextKitComponents.textStorage, attributedPlaceholderText))
return;
[_placeholderTextKitComponents.textStorage setAttributedString:attributedPlaceholderText ? : [[NSAttributedString alloc] initWithString:@""]];
_textKitComponents.textView.accessibilityHint = attributedPlaceholderText.string;
}
#pragma mark - Modifying User Text
@dynamic attributedText;
- (NSAttributedString *)attributedText
{
// Per contract in our header, this value is nil when the placeholder is displayed.
if ([self isDisplayingPlaceholder])
return nil;
ASDN::MutexLocker l(_textKitLock);
return [_textKitComponents.textStorage copy];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
ASDN::MutexLocker l(_textKitLock);
// If we (_cmd) are called while the text view itself is updating (-textViewDidUpdate:), you cannot update the text storage and expect perfect propagation to the text view.
// Thus, we always update the textview directly if it's been created already.
if (ASObjectIsEqual((_textKitComponents.textView.attributedText ? : _textKitComponents.textStorage), attributedText))
return;
// If the cursor isn't at the end of the text, we need to preserve the selected range to avoid moving the cursor.
NSRange selectedRange = _textKitComponents.textView.selectedRange;
BOOL preserveSelectedRange = (selectedRange.location != _textKitComponents.textStorage.length);
NSAttributedString *attributedStringToDisplay = nil;
if (attributedText)
attributedStringToDisplay = attributedText;
// Otherwise, note that we don't simply nil out attributed text. Because the insertion point is guided by the attributes at index 0, we need to attribute an empty string to ensure the insert point obeys our typing attributes.
else
attributedStringToDisplay = [[NSAttributedString alloc] initWithString:@"" attributes:self.typingAttributes];
// Always prefer updating the text view directly if it's been created (see above).
if (_textKitComponents.textView)
[_textKitComponents.textView setAttributedText:attributedStringToDisplay];
else
[_textKitComponents.textStorage setAttributedString:attributedStringToDisplay];
// Calculated size depends on the seeded text.
[self setNeedsLayout];
// Update if placeholder is shown.
[self _updateDisplayingPlaceholder];
// Preserve cursor range, if necessary.
if (preserveSelectedRange) {
_isPreservingSelection = YES; // Used in -textViewDidChangeSelection: to avoid informing our delegate about our preservation.
[_textKitComponents.textView setSelectedRange:selectedRange];
_isPreservingSelection = NO;
}
}
#pragma mark - Core
- (void)_updateDisplayingPlaceholder
{
ASDN::MutexLocker l(_textKitLock);
// Show the placeholder if necessary.
_displayingPlaceholder = (_textKitComponents.textStorage.length == 0);
_placeholderTextKitComponents.textView.hidden = !_displayingPlaceholder;
// If hiding the placeholder, propagate backgroundColor/opaque to the editable textView. It is positioned over the placeholder to accept taps to begin editing, and was kept transparent so it doesn't obscure the placeholder text. Now that we're editing it and the placeholder is hidden, we can make it opaque to avoid unnecessary blending.
if (!_displayingPlaceholder) {
_textKitComponents.textView.opaque = self.isOpaque;
_textKitComponents.textView.backgroundColor = self.backgroundColor;
} else {
_textKitComponents.textView.opaque = NO;
_textKitComponents.textView.backgroundColor = nil;
}
}
- (void)_layoutTextView
{
ASDN::MutexLocker l(_textKitLock);
// Layout filling our bounds.
_textKitComponents.textView.frame = self.bounds;
_placeholderTextKitComponents.textView.frame = self.bounds;
// Note that both of these won't be necessary once we can disable scrolling, pending rdar://14729288
// When we resize to fit (above) the prior layout becomes invalid. For whatever reason, UITextView doesn't invalidate its layout when its frame changes on its own, so we have to do so ourselves.
[_textKitComponents.layoutManager invalidateLayoutForCharacterRange:NSMakeRange(0, [_textKitComponents.textStorage length]) actualCharacterRange:NULL];
// When you type beyond UITextView's bounds it scrolls you down a line. We need to remain at the top.
[_textKitComponents.textView setContentOffset:CGPointZero animated:NO];
}
#pragma mark - Keyboard
@dynamic textInputMode;
- (UITextInputMode *)textInputMode
{
ASDN::MutexLocker l(_textKitLock);
return [_textKitComponents.textView textInputMode];
}
- (BOOL)isFirstResponder
{
ASDN::MutexLocker l(_textKitLock);
return [_textKitComponents.textView isFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
ASDN::MutexLocker l(_textKitLock);
return [_textKitComponents.textView canBecomeFirstResponder];
}
- (BOOL)becomeFirstResponder
{
ASDN::MutexLocker l(_textKitLock);
return [_textKitComponents.textView becomeFirstResponder];
}
- (BOOL)canResignFirstResponder {
ASDN::MutexLocker l(_textKitLock);
return [_textKitComponents.textView canResignFirstResponder];
}
- (BOOL)resignFirstResponder
{
ASDN::MutexLocker l(_textKitLock);
return [_textKitComponents.textView resignFirstResponder];
}
#pragma mark - UITextInputTraits
- (_ASTextInputTraitsPendingState *)textInputTraits
{
if (!_textInputTraits) {
_textInputTraits = [[_ASTextInputTraitsPendingState alloc] init];
}
return _textInputTraits;
}
- (void)setAutocapitalizationType:(UITextAutocapitalizationType)autocapitalizationType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setAutocapitalizationType:autocapitalizationType];
} else {
[self.textInputTraits setAutocapitalizationType:autocapitalizationType];
}
}
- (UITextAutocapitalizationType)autocapitalizationType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView autocapitalizationType];
} else {
return [self.textInputTraits autocapitalizationType];
}
}
- (void)setAutocorrectionType:(UITextAutocorrectionType)autocorrectionType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setAutocorrectionType:autocorrectionType];
} else {
[self.textInputTraits setAutocorrectionType:autocorrectionType];
}
}
- (UITextAutocorrectionType)autocorrectionType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView autocorrectionType];
} else {
return [self.textInputTraits autocorrectionType];
}
}
- (void)setSpellCheckingType:(UITextSpellCheckingType)spellCheckingType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setSpellCheckingType:spellCheckingType];
} else {
[self.textInputTraits setSpellCheckingType:spellCheckingType];
}
}
- (UITextSpellCheckingType)spellCheckingType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView spellCheckingType];
} else {
return [self.textInputTraits spellCheckingType];
}
}
- (void)setEnablesReturnKeyAutomatically:(BOOL)enablesReturnKeyAutomatically
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setEnablesReturnKeyAutomatically:enablesReturnKeyAutomatically];
} else {
[self.textInputTraits setEnablesReturnKeyAutomatically:enablesReturnKeyAutomatically];
}
}
- (BOOL)enablesReturnKeyAutomatically
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView enablesReturnKeyAutomatically];
} else {
return [self.textInputTraits enablesReturnKeyAutomatically];
}
}
- (void)setKeyboardAppearance:(UIKeyboardAppearance)setKeyboardAppearance
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setKeyboardAppearance:setKeyboardAppearance];
} else {
[self.textInputTraits setKeyboardAppearance:setKeyboardAppearance];
}
}
- (UIKeyboardAppearance)keyboardAppearance
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView keyboardAppearance];
} else {
return [self.textInputTraits keyboardAppearance];
}
}
- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setKeyboardType:keyboardType];
} else {
[self.textInputTraits setKeyboardType:keyboardType];
}
}
- (UIKeyboardType)keyboardType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView keyboardType];
} else {
return [self.textInputTraits keyboardType];
}
}
- (void)setReturnKeyType:(UIReturnKeyType)returnKeyType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setReturnKeyType:returnKeyType];
} else {
[self.textInputTraits setReturnKeyType:returnKeyType];
}
}
- (UIReturnKeyType)returnKeyType
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView returnKeyType];
} else {
return [self.textInputTraits returnKeyType];
}
}
- (void)setSecureTextEntry:(BOOL)secureTextEntry
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
[self.textView setSecureTextEntry:secureTextEntry];
} else {
[self.textInputTraits setSecureTextEntry:secureTextEntry];
}
}
- (BOOL)isSecureTextEntry
{
ASDN::MutexLocker l(_textInputTraitsLock);
if (self.isNodeLoaded) {
return [self.textView isSecureTextEntry];
} else {
return [self.textInputTraits isSecureTextEntry];
}
}
#pragma mark - UITextView Delegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// Delegateify.
return [self _delegateShouldBeginEditing];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// Delegateify.
[self _delegateDidBeginEditing];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// Delegateify.
return [self _delegateShouldChangeTextInRange:range replacementText:text];
}
- (void)textViewDidChange:(UITextView *)textView
{
ASDN::MutexLocker l(_textKitLock);
// Note we received a text changed event.
// This is used by _delegateDidChangeSelectionFromSelectedRange:toSelectedRange: to distinguish between selection changes that happen because of editing or pure selection changes.
_selectionChangedForEditedText = YES;
// Update if the placeholder is visible.
[self _updateDisplayingPlaceholder];
// Invalidate, as our calculated size depends on the textview's seeded text.
[self invalidateCalculatedLayout];
// Delegateify.
[self _delegateDidUpdateText];
}
- (void)textViewDidChangeSelection:(UITextView *)textView
{
// Typing attributes get reset when selection changes. Reapply them so they actually obey our header.
_textKitComponents.textView.typingAttributes = _typingAttributes;
// If we're only changing selection to preserve it, don't notify about anything.
if (_isPreservingSelection)
return;
// Note if we receive a -textDidChange: between now and when we delegatify.
// This is used by _delegateDidChangeSelectionFromSelectedRange:toSelectedRange: to distinguish between selection changes that happen because of editing or pure selection changes.
_selectionChangedForEditedText = NO;
NSRange fromSelectedRange = _previousSelectedRange;
NSRange toSelectedRange = self.selectedRange;
_previousSelectedRange = toSelectedRange;
// Delegateify.
[self _delegateDidChangeSelectionFromSelectedRange:fromSelectedRange toSelectedRange:toSelectedRange];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
// Delegateify.
[self _delegateDidFinishEditing];
}
#pragma mark - NSLayoutManager Delegate
- (NSUInteger)layoutManager:(NSLayoutManager *)layoutManager shouldGenerateGlyphs:(const CGGlyph *)glyphs properties:(const NSGlyphProperty *)properties characterIndexes:(const NSUInteger *)characterIndexes font:(UIFont *)aFont forGlyphRange:(NSRange)glyphRange
{
return [_wordKerner layoutManager:layoutManager shouldGenerateGlyphs:glyphs properties:properties characterIndexes:characterIndexes font:aFont forGlyphRange:glyphRange];
}
- (NSControlCharacterAction)layoutManager:(NSLayoutManager *)layoutManager shouldUseAction:(NSControlCharacterAction)defaultAction forControlCharacterAtIndex:(NSUInteger)characterIndex
{
return [_wordKerner layoutManager:layoutManager shouldUseAction:defaultAction forControlCharacterAtIndex:characterIndex];
}
- (CGRect)layoutManager:(NSLayoutManager *)layoutManager boundingBoxForControlGlyphAtIndex:(NSUInteger)glyphIndex forTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)proposedRect glyphPosition:(CGPoint)glyphPosition characterIndex:(NSUInteger)characterIndex
{
return [_wordKerner layoutManager:layoutManager boundingBoxForControlGlyphAtIndex:glyphIndex forTextContainer:textContainer proposedLineFragment:proposedRect glyphPosition:glyphPosition characterIndex:characterIndex];
}
#pragma mark - Geometry
- (CGRect)frameForTextRange:(NSRange)textRange
{
ASDN::MutexLocker l(_textKitLock);
// Bail on invalid range.
if (NSMaxRange(textRange) > [_textKitComponents.textStorage length]) {
ASDisplayNodeAssert(NO, @"Invalid range");
return CGRectZero;
}
// Force glyph generation and layout.
[_textKitComponents.layoutManager ensureLayoutForTextContainer:_textKitComponents.textContainer];
NSRange glyphRange = [_textKitComponents.layoutManager glyphRangeForCharacterRange:textRange actualCharacterRange:NULL];
CGRect textRect = [_textKitComponents.layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:_textKitComponents.textContainer];
return [_textKitComponents.textView convertRect:textRect toView:self.view];
}
#pragma mark -
- (BOOL)_delegateShouldBeginEditing
{
if ([_delegate respondsToSelector:@selector(editableTextNodeShouldBeginEditing:)]) {
return [_delegate editableTextNodeShouldBeginEditing:self];
}
return YES;
}
- (void)_delegateDidBeginEditing
{
if ([_delegate respondsToSelector:@selector(editableTextNodeDidBeginEditing:)])
[_delegate editableTextNodeDidBeginEditing:self];
}
- (BOOL)_delegateShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([_delegate respondsToSelector:@selector(editableTextNode:shouldChangeTextInRange:replacementText:)]) {
return [_delegate editableTextNode:self shouldChangeTextInRange:range replacementText:text];
}
return YES;
}
- (void)_delegateDidChangeSelectionFromSelectedRange:(NSRange)fromSelectedRange toSelectedRange:(NSRange)toSelectedRange
{
// There are two reasons we're invoking the delegate on the next run of the runloop.
// 1. UITextView invokes its delegate methods when it's in the middle of text-processing. For example, -textViewDidChange: is invoked before you can truly rely on the changes being propagated throughout the Text Kit hierarchy.
// 2. This delegate method (-textViewDidChangeSelection:) is called both before -textViewDidChange: and before the layout manager/etc. has necessarily generated+laid out its glyphs. Because of the former, we need to wait until -textViewDidChange: has had an opportunity to be called so can accurately determine whether this selection change is due to editing (_selectionChangedForEditedText).
// Thus, to avoid calling out to client code in the middle of UITextView's processing, we call the delegate on the next run of the runloop, when all such internal processing is surely done.
dispatch_async(dispatch_get_main_queue(), ^{
if ([_delegate respondsToSelector:@selector(editableTextNodeDidChangeSelection:fromSelectedRange:toSelectedRange:dueToEditing:)])
[_delegate editableTextNodeDidChangeSelection:self fromSelectedRange:fromSelectedRange toSelectedRange:toSelectedRange dueToEditing:_selectionChangedForEditedText];
});
}
- (void)_delegateDidUpdateText
{
// Note that because -editableTextNodeDidUpdateText: passes no state, the current state of the receiver will be accessed. Thus, it's not useful to enqueue a second delegation call if the first hasn't happened yet -- doing so will result in the delegate receiving -editableTextNodeDidUpdateText: when the "updated text" has already been processed. This may sound innocuous, but because our delegation may cause additional updates to the textview's string, and because such updates discard spelling suggestions and autocompletions (like double-space to `.`), it can actually be quite dangerous!
if (_delegateDidUpdateEnqueued)
return;
_delegateDidUpdateEnqueued = YES;
// UITextView invokes its delegate methods when it's in the middle of text-processing. For example, -textViewDidChange: is invoked before you can truly rely on the changes being propagated throughout the Text Kit hierarchy.
// Thus, to avoid calling out to client code in the middle of UITextView's processing, we call the delegate on the next run of the runloop, when all such internal processing is surely done.
dispatch_async(dispatch_get_main_queue(), ^{
_delegateDidUpdateEnqueued = NO;
if ([_delegate respondsToSelector:@selector(editableTextNodeDidUpdateText:)])
[_delegate editableTextNodeDidUpdateText:self];
});
}
- (void)_delegateDidFinishEditing
{
if ([_delegate respondsToSelector:@selector(editableTextNodeDidFinishEditing:)])
[_delegate editableTextNodeDidFinishEditing:self];
}
@end