IOS 8 UITextView scrolling to caret after inserting newline

The automatic scroll to caret behavior of iOS 8 is UITextView

better than iOS 7 UITextView

. However, I still see a problem in the behavior of scrolling to carriage when the text of the text view contains a newline character ( \n

).

Here's the problem described in the screenshots:

enter image description here

ABOVE: UITextView

Displays Latin text by default. Note that laborum

I inserted a newline after the word . The text was set in the code.

enter image description here

ABOVE: The user presses a space after the last word, the civiuda

keyboard pops up and the text view automatically scrolls to the caret. This is expected.

enter image description here

ABOVE: The user presses the return button on the keyboard and the carriage is lost behind the keyboard. This is an unexpected problem.

If I remove the newline character from the text view text, the problem goes away. Moreover, if I move the newline closer to the beginning of the text (e.g. after the first word Lorem

), the problem also disappears.

Am I doing something wrong? If not, can anyone suggest a fix?

UITextView

created in IB using Autolayout.

enter image description here

Here's the code:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // text contains a newline character after the word 'laborum'; removing the newline character eliminates the scrolling issue
    self.textView.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\nNam liber te conscient to factor tum poen legum odioque civiuda.";

    self.textView.font = [UIFont systemFontOfSize:26];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
}

- (void)keyboardDidShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardRect = [self.textView convertRect:keyboardRect fromView:nil];

    UIEdgeInsets insets = self.textView.contentInset;
    insets.bottom = keyboardRect.size.height;
    self.textView.contentInset = insets;
    self.textView.scrollIndicatorInsets = insets;

    // doesn't fix the scrolling issue
    [self.textView setNeedsLayout];
}

      

What I have tried:

Instead of using it, UITextView'

contentInset

I tried to use the bottom constraint to push the text content above the keyboard. The same scrolling.

I created an instance UITextView

in code. The same scrolling.

It has been suggested that pressing the return key might be misinterpreted as a ready-made edit key by the system. I went ahead and registered for keyboard hide notifications. They are not called.

+3


source to share


1 answer


Try the following:



#param mark - UITextViewDelegate

- (void)textViewDidChangeSelection:(UITextView *)textView
{
    if (IOS8) {
        [self.textView scrollRangeToVisible:self.textView.selectedRange];
    }
}

      

0


source







All Articles