Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 053f99a

Browse files
author
dzenbot
committed
Gets rid of hacky implementation to detect text deletion and tweak auto-completion behaviors. This might fix strange behaviors while typing text.
1 parent 3ac2744 commit 053f99a

3 files changed

Lines changed: 5 additions & 107 deletions

File tree

Source/Classes/SLKTextView.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ extern NSString * const SLKTextViewTextWillChangeNotification;
2020
extern NSString * const SLKTextViewContentSizeDidChangeNotification;
2121
extern NSString * const SLKTextViewDidPasteItemNotification;
2222
extern NSString * const SLKTextViewDidShakeNotification;
23-
extern NSString * const SLKTextViewDidFinishDeletingNotification;
2423

2524
extern NSString * const SLKTextViewPastedItemContentType;
2625
extern NSString * const SLKTextViewPastedItemMediaType;
@@ -72,9 +71,6 @@ typedef NS_OPTIONS(NSUInteger, SLKPastableMediaType) {
7271
/** YES if the text view supports undoing, either using UIMenuController, or with ctrl+z when using an external keyboard. Default is YES. */
7372
@property (nonatomic, readwrite) BOOL undoManagerEnabled;
7473

75-
/** */
76-
@property (nonatomic, getter=isFastDeleting) BOOL fastDeleting;
77-
7874
/**
7975
Some text view properties don't update when it's already firstResponder (auto-correction, spelling-check, etc.)
8076
To be able to update the text view while still being first responder, requieres to switch quickly from -resignFirstResponder to -becomeFirstResponder.

Source/Classes/SLKTextView.m

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,12 @@
2222
NSString * const SLKTextViewContentSizeDidChangeNotification = @"SLKTextViewContentSizeDidChangeNotification";
2323
NSString * const SLKTextViewDidPasteItemNotification = @"SLKTextViewDidPasteItemNotification";
2424
NSString * const SLKTextViewDidShakeNotification = @"SLKTextViewDidShakeNotification";
25-
NSString * const SLKTextViewDidFinishDeletingNotification = @"SLKTextViewDidFinishDeletingNotification";
2625

2726
NSString * const SLKTextViewPastedItemContentType = @"SLKTextViewPastedItemContentType";
2827
NSString * const SLKTextViewPastedItemMediaType = @"SLKTextViewPastedItemMediaType";
2928
NSString * const SLKTextViewPastedItemData = @"SLKTextViewPastedItemData";
3029

31-
static NSTimeInterval kDeleteMaxTimeInterval = 0.5;
32-
3330
@interface SLKTextView ()
34-
{
35-
NSTimeInterval _lastDeletionTimeInterval;
36-
}
3731

3832
// The label used as placeholder
3933
@property (nonatomic, strong) UILabel *placeholderLabel;
@@ -49,9 +43,6 @@ @interface SLKTextView ()
4943
// Used for detecting if the scroll indicator was previously flashed
5044
@property (nonatomic) BOOL didFlashScrollIndicators;
5145

52-
// Used to refresh the first responder's
53-
@property (nonatomic, strong) NSTimer *deletionTimer;
54-
5546
@end
5647

5748
@implementation SLKTextView
@@ -146,15 +137,6 @@ - (UILabel *)placeholderLabel
146137
return _placeholderLabel;
147138
}
148139

149-
- (NSTimer *)deletionTimer
150-
{
151-
if (!_deletionTimer) {
152-
_deletionTimer = [NSTimer timerWithTimeInterval:kDeleteMaxTimeInterval target:self selector:@selector(_shouldRefreshFirstResponder:) userInfo:nil repeats:NO];
153-
[[NSRunLoop currentRunLoop] addTimer:_deletionTimer forMode:NSRunLoopCommonModes];
154-
}
155-
return _deletionTimer;
156-
}
157-
158140
- (NSString *)placeholder
159141
{
160142
return self.placeholderLabel.text;
@@ -369,22 +351,6 @@ - (void)setUndoManagerEnabled:(BOOL)enabled
369351
_undoManagerEnabled = enabled;
370352
}
371353

372-
- (void)_invalidateDeletionTimer
373-
{
374-
if (!_deletionTimer) {
375-
return;
376-
}
377-
378-
[_deletionTimer invalidate];
379-
_deletionTimer = nil;
380-
}
381-
382-
- (void)_invalidateFastDeletion
383-
{
384-
_fastDeleting = NO;
385-
_lastDeletionTimeInterval = 0;
386-
}
387-
388354

389355
#pragma mark - UITextView Overrides
390356

@@ -428,46 +394,6 @@ - (void)setTextAlignment:(NSTextAlignment)textAlignment
428394
- (void)insertText:(NSString *)text
429395
{
430396
[super insertText:text];
431-
432-
[self _invalidateFastDeletion];
433-
}
434-
435-
- (void)deleteBackward
436-
{
437-
// No need to call super on iOS8, or it will delete 2 characters at once
438-
if (!SLK_IS_IOS8_AND_HIGHER) {
439-
[super deleteBackward];
440-
}
441-
442-
NSTimeInterval deletionTimestamp = [[NSDate date] timeIntervalSince1970];
443-
NSTimeInterval delta = roundf(100.0 * (deletionTimestamp - _lastDeletionTimeInterval)) / 100.0;
444-
445-
_fastDeleting = (self.hasText && delta <= kDeleteMaxTimeInterval);
446-
_lastDeletionTimeInterval = deletionTimestamp;
447-
448-
// Makes sure the first responder is refreshed when the user released the backward key
449-
if (self.isFastDeleting) {
450-
[self _invalidateDeletionTimer];
451-
[self deletionTimer];
452-
}
453-
}
454-
455-
// Used on iOS8 to trigger 'deleteBackward' since it's no longer called by super.
456-
// 'keyboardInputShouldDelete:' should be safe, since it doesn't call super, but it is still considered a private API.
457-
- (BOOL)keyboardInputShouldDelete:(__unused id <UITextInput>)textInput
458-
{
459-
if (SLK_IS_IOS8_AND_HIGHER) {
460-
[self deleteBackward];
461-
}
462-
463-
if (self.delegate && [self.delegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
464-
NSRange range = self.selectedRange;
465-
if (range.location > 0) range.location--;
466-
if (range.length == 0 && self.text.length > 0) range.length++;
467-
return [self.delegate textView:self shouldChangeTextInRange:range replacementText:@"^H"];
468-
}
469-
470-
return self.hasText;
471397
}
472398

473399

@@ -587,19 +513,6 @@ - (void)setTypingSuggestionEnabled:(BOOL)enabled
587513

588514
self.autocorrectionType = enabled ? UITextAutocorrectionTypeDefault : UITextAutocorrectionTypeNo;
589515
self.spellCheckingType = enabled ? UITextSpellCheckingTypeDefault : UITextSpellCheckingTypeNo;
590-
591-
if (!self.isFastDeleting) {
592-
[self refreshFirstResponder];
593-
}
594-
}
595-
596-
- (void)_shouldRefreshFirstResponder:(NSTimer *)timer
597-
{
598-
[self refreshFirstResponder];
599-
[self _invalidateDeletionTimer];
600-
[self _invalidateFastDeletion];
601-
602-
[[NSNotificationCenter defaultCenter] postNotificationName:SLKTextViewDidFinishDeletingNotification object:self];
603516
}
604517

605518
- (void)refreshFirstResponder
@@ -856,9 +769,6 @@ - (void)dealloc
856769

857770
[self removeObserver:self forKeyPath:NSStringFromSelector(@selector(contentSize))];
858771

859-
[self _invalidateDeletionTimer];
860-
[self _invalidateFastDeletion];
861-
862772
_placeholderLabel = nil;
863773
}
864774

Source/Classes/SLKTextViewController.m

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,17 @@ - (void)viewDidDisappear:(BOOL)animated
210210

211211
- (void)viewWillLayoutSubviews
212212
{
213+
NSLog(@"%s",__FUNCTION__);
214+
213215
[super viewWillLayoutSubviews];
214216

215217
[self _adjustContentConfigurationIfNeeded];
216218
}
217219

218220
- (void)viewDidLayoutSubviews
219221
{
222+
NSLog(@"%s",__FUNCTION__);
223+
220224
[super viewDidLayoutSubviews];
221225
}
222226

@@ -1310,16 +1314,6 @@ - (void)_didShakeTextView:(NSNotification *)notification
13101314
}
13111315
}
13121316

1313-
- (void)_didDeleteInTextView:(NSNotification *)notification
1314-
{
1315-
// Skips this if it's not the expected textView.
1316-
if (![self.textView isFirstResponder]) {
1317-
return;
1318-
}
1319-
1320-
[self _processTextForAutoCompletion];
1321-
}
1322-
13231317
- (void)_willShowOrHideTypeIndicatorView:(NSNotification *)notification
13241318
{
13251319
SLKTypingIndicatorView *indicatorView = (SLKTypingIndicatorView *)notification.object;
@@ -1376,7 +1370,7 @@ - (void)registerPrefixesForAutoCompletion:(NSArray *)prefixes
13761370

13771371
- (void)_processTextForAutoCompletion
13781372
{
1379-
if (self.isRotating || self.textView.isFastDeleting) {
1373+
if (self.isRotating) {
13801374
return;
13811375
}
13821376

@@ -1871,7 +1865,6 @@ - (void)_registerNotifications
18711865
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didChangeTextViewContentSize:) name:SLKTextViewContentSizeDidChangeNotification object:nil];
18721866
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didChangeTextViewPasteboard:) name:SLKTextViewDidPasteItemNotification object:nil];
18731867
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didShakeTextView:) name:SLKTextViewDidShakeNotification object:nil];
1874-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didDeleteInTextView:) name:SLKTextViewDidFinishDeletingNotification object:nil];
18751868

18761869
// TypeIndicator notifications
18771870
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_willShowOrHideTypeIndicatorView:) name:SLKTypingIndicatorViewWillShowNotification object:nil];
@@ -1908,7 +1901,6 @@ - (void)_unregisterNotifications
19081901
[[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTextViewContentSizeDidChangeNotification object:nil];
19091902
[[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTextViewDidPasteItemNotification object:nil];
19101903
[[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTextViewDidShakeNotification object:nil];
1911-
[[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTextViewDidFinishDeletingNotification object:nil];
19121904

19131905
// TypeIndicator notifications
19141906
[[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTypingIndicatorViewWillShowNotification object:nil];

0 commit comments

Comments
 (0)