简体   繁体   中英

iOS 8 UITextView scroll-to-caret issue after inserting newline character

The automatic scroll-to-caret behavior of an iOS 8 UITextView is better than that of an iOS 7 UITextView . However, I'm still seeing an issue in the scroll-to-caret behavior when the text view's text contains a newline character ( \\n ).

Here's the issue described in screenshots:

在此输入图像描述

ABOVE: UITextView 's default Latin text is displayed. Notice that I inserted a newline character after the word laborum . The text was set in code.

在此输入图像描述

ABOVE: The user clicks in the space after the last word, civiuda , the keyboard pops up, and the text view automatically scrolls to the caret. This is expected.

在此输入图像描述

ABOVE: The user taps the return button on the keyboard and the caret is lost behind the keyboard. This is an unexpected issue.

If I remove the newline character from the text view's text, the issue disappears. Moreover, if I move the newline character closer to the beginning of the text (eg, after the first word, Lorem ), the issue also disappears.

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

The UITextView is instantiated in IB using Autolayout.

在此输入图像描述

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 the UITextView' s contentInset , I tried using a bottom constraint to push the text content above the keyboard. Same scrolling issue.

I instantiated the UITextView in code. Same scrolling issue.

It was suggested that tapping the return key may be misinterpreted as the done-editing key by the system. I went ahead and registered for the keyboard hide notifications. They're not being called.

Try this:

#param mark - UITextViewDelegate

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM