diff --git a/Chromatism/Chromatism/JLTextViewController.m b/Chromatism/Chromatism/JLTextViewController.m index abacff9..2f77cc5 100644 --- a/Chromatism/Chromatism/JLTextViewController.m +++ b/Chromatism/Chromatism/JLTextViewController.m @@ -107,20 +107,33 @@ - (void)keyboardWasShown:(NSNotification *)notification UIScrollView *scrollView = self.textView; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; - UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); + UIEdgeInsets contentInsets = scrollView.contentInset; + contentInsets.bottom = kbSize.height; scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; - //CGPoint caretPosition = [self.textView caretRectForPosition:self.textView.selectedTextRange.start].origin; - //CGRect caretRect = CGRectMake(caretPosition.x, caretPosition.y, 1, 1); - //[self.textView scrollRectToVisible:caretRect animated:YES]; + CGPoint point = [self.textView caretRectForPosition:self.textView.selectedTextRange.start].origin; + point.y = MIN(point.y, self.textView.frame.size.height - kbSize.height); + + CGRect aRect = self.view.frame; + aRect.size.height -= kbSize.height; + if (!CGRectContainsPoint(aRect, point) ) { + + CGRect rect = CGRectMake(point.x, point.y, 1, 1); + rect.size.height = kbSize.height; + rect.origin.y += kbSize.height; + [self.textView scrollRectToVisible:rect animated:YES]; + } } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification *)notification { UIScrollView *scrollView = self.textView; - UIEdgeInsets contentInsets = UIEdgeInsetsZero; + UIEdgeInsets contentInsets = scrollView.contentInset; + contentInsets.bottom = 0; + scrollView.contentInset = contentInsets; + scrollView.scrollIndicatorInsets = contentInsets; scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; } diff --git a/ChromatismDemo/ChromatismDemo/AppDelegate.m b/ChromatismDemo/ChromatismDemo/AppDelegate.m index ac354dd..3c276d4 100644 --- a/ChromatismDemo/ChromatismDemo/AppDelegate.m +++ b/ChromatismDemo/ChromatismDemo/AppDelegate.m @@ -36,6 +36,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSString *string = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:URL] encoding:NSUTF8StringEncoding]; JLTextViewController *viewController = [[JLTextViewController alloc] initWithText:string]; + viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Resign" style:UIBarButtonItemStylePlain target:viewController.textView action:@selector(resignFirstResponder)]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; viewController.title = @"Chromatism";