Skip to content

Obsolete code in handleTextViewDidChangeNotification #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zyavrik opened this issue Jun 7, 2014 · 3 comments
Open

Obsolete code in handleTextViewDidChangeNotification #20

zyavrik opened this issue Jun 7, 2014 · 3 comments

Comments

@zyavrik
Copy link

zyavrik commented Jun 7, 2014

The following code is obsolete regarding three commended lines:

- (void)handleTextViewDidChangeNotification:(NSNotification *)notification
{
if (notification.object == self)
{
    CGRect line = [self caretRectForPosition: self.selectedTextRange.start];
    CGFloat overflow = line.origin.y + line.size.height - ( self.contentOffset.y + self.bounds.size.height - self.contentInset.bottom - self.contentInset.top );

    if ( overflow > 0 )
    {
        // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
        // Scroll caret to visible area
        CGPoint offset = self.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
        // Cannot animate with setContentOffset:animated: or caret will not appear
//            [UIView animateWithDuration:.2 animations:^{
//                [self setContentOffset:offset];
//            }];
    }
}
}
@zyavrik zyavrik changed the title Obsolete code in handleTextViewDidChangeNotification:(NSNotification *)notification Obsolete code in handleTextViewDidChangeNotification Jun 7, 2014
@zyavrik
Copy link
Author

zyavrik commented Jun 8, 2014

The initial issue and correct fix for iOS 7.x are described here: http://stackoverflow.com/questions/22315755/ios-7-1-uitextview-still-not-scrolling-to-cursor-caret-after-new-line?lq=1 On iOS 8 the issue is fixed.

@zyavrik
Copy link
Author

zyavrik commented Jun 8, 2014

@zyavrik
Copy link
Author

zyavrik commented Jun 8, 2014

Most correct fix:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define is_iOS7 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
#define is_iOS8 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")

@implementation CYRTextView {
    ...old code...
    BOOL settingText;
}

- (id)initWithFrame:(CGRect)frame {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextViewDidChangeNotification:) name:UITextViewTextDidChangeNotification object:self];
}

- (void)scrollToCaretInTextView:(UITextView *)textView animated:(BOOL)animated {
    CGRect rect = [textView caretRectForPosition:textView.selectedTextRange.end];
    rect.size.height += textView.textContainerInset.bottom;
    [textView scrollRectToVisible:rect animated:animated];
}

- (void)handleTextViewDidChangeNotification:(NSNotification *)notification {
    if (notification.object == self && is_iOS7 && !is_iOS8 && !settingText) {
        UITextView *textView = self;
        if ([textView.text hasSuffix:@"\n"]) {
            [CATransaction setCompletionBlock:^{
                [self scrollToCaretInTextView:textView animated:NO];
            }];
        } else {
            [self scrollToCaretInTextView:textView animated:NO];
        }
    }
}

- (void)setText:(NSString *)text {
    settingText = YES;
   ...old code..
    settingText = NO;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant