UITextView contentSize changes and NSLayoutManager in iOS7

后端 未结 3 1033
不思量自难忘°
不思量自难忘° 2021-02-06 04:20

The problem: UITextView silently changes it\'s contentSize in some situations.

The simplest case textView with large text and keyboard. Just ad

3条回答
  •  佛祖请我去吃肉
    2021-02-06 04:50

    Seems the bug in iOS7. At the time of input text content area behavior is wired in iOS7, It works fine with lower iOS7 version.

    I have added below delegate method of UITextView to resolved this issue :

    - (void)textViewDidChange:(UITextView *)textView {
    CGRect line = [textView caretRectForPosition:
        textView.selectedTextRange.start];
    CGFloat overflow = line.origin.y + line.size.height
        - ( textView.contentOffset.y + textView.bounds.size.height
        - textView.contentInset.bottom - textView.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 = textView.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
    // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [textView setContentOffset:offset];
        }];
    }
    

提交回复
热议问题