TextKit を使用して、ページ化された UIScrollView を作成し、その内容をテキストで埋めます。すべてのページングは完全に機能します。私が困惑したのは、すべてのテキストが 1 ページに収まると、すべてが期待どおりに機能するということです。ただし、テキストが長く、スクロールビューで複数のページを作成する必要がある場合、テキストは強調表示/選択できなくなります。
簡単な例を次に示します (これを新しいプロジェクトに挿入しviewDidLoad
て複製するだけです)。すべてのテキストが 1 ページに収まるように、テキスト ビューのフォント サイズを小さいサイズ (12pt) に変更すると、期待どおりの動作が得られます。逆に、大きいサイズ (22pt) では、スクロールビュー用に複数のページが作成され、テキストの扱いにくさが失われます。
// Create a scroll view:
UIScrollView *scrollingView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64)]; // we will dynamically set the content size later
scrollingView.pagingEnabled = YES;
scrollingView.backgroundColor = [UIColor clearColor];
scrollingView.userInteractionEnabled = YES;
scrollingView.delaysContentTouches = NO;
[self.view addSubview:scrollingView];
// Set up text storage and add string:
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum."
attributes:@{NSForegroundColorAttributeName: [UIColor blueColor],
NSFontAttributeName: [UIFont systemFontOfSize:22]}]];
// Create a layour manager:
NSLayoutManager *textLayout = [[NSLayoutManager alloc] init];
// Add layout manager to text storage object:
[textStorage addLayoutManager:textLayout];
// Create text containers and views, adding each container to the layout manager:
NSUInteger lastRenderedGlyph = 0;
CGFloat currentYOffset = 0;
NSInteger pageNumber = 1;
while (lastRenderedGlyph < textLayout.numberOfGlyphs) {
CGRect textViewFrame = CGRectMake(0, currentYOffset, scrollingView.frame.size.width, scrollingView.frame.size.height);
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:scrollingView.frame.size];
[textLayout addTextContainer:textContainer];
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame textContainer:textContainer];
textView.backgroundColor = [UIColor clearColor];
[textView setTag:pageNumber];
textView.scrollEnabled = NO;
textView.editable = NO;
textView.userInteractionEnabled = YES;
textView.selectable = YES;
textView.font = [UIFont systemFontOfSize:22]; // Change between small sizes and large sizes to replicate bug.
[scrollingView addSubview:textView];
// Increase current offset:
currentYOffset += CGRectGetHeight(textViewFrame);
// Find the index of the glyph we've just rendered:
lastRenderedGlyph = NSMaxRange([textLayout glyphRangeForTextContainer:textContainer]);
pageNumber++;
}
// Set the content size of the scroll view to fit the length of the text:
CGSize contentSize = CGSizeMake(CGRectGetWidth(scrollingView.bounds), currentYOffset);
scrollingView.contentSize = contentSize;
私の質問は、テキストに複数のページが必要な場合でも、虫眼鏡と編集メニューの機能を元に戻すにはどうすればよいですか?
助けてくれてありがとう!