iOS7 UITExtView 커서 문제

2638 단어 UITextView
최근 프로젝트에서 UITextView가 ios7에 편집자가 마지막 줄에 들어갈 때 커서가 사라지고 마지막 줄을 볼 수 없어 맹타로 변하는 것을 만났다. Stack OverFlow 사이트에 신이 지적한 것은 ios7 자체 버그이고 다음 코드를 더하면 된다.
 1 -(void)textViewDidChange:(UITextView *)textView {

 2     CGRect line = [textView caretRectForPosition:

 3                    textView.selectedTextRange.start];

 4     CGFloat overflow = line.origin.y + line.size.height

 5     - ( textView.contentOffset.y + textView.bounds.size.height

 6        - textView.contentInset.bottom - textView.contentInset.top );

 7     if ( overflow > 0 ) {

 8         // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)

 9         // Scroll caret to visible area

10         CGPoint offset = textView.contentOffset;

11         offset.y += overflow + 7; // leave 7 pixels margin

12         // Cannot animate with setContentOffset:animated: or caret will not appear

13         [UIView animateWithDuration:.2 animations:^{

14             [textView setContentOffset:offset];

15         }];

16     }

17 }

 

좋은 웹페이지 즐겨찾기