我创建了一个分页 UIScrollView 并使用 TextKit 用文本填充其内容。所有的分页工作完美。让我难过的是,当所有文本都适合一页时,一切都按预期工作。但是,如果文本较长并且需要滚动视图来创建多个页面,则文本不再是可突出显示/可选择的。
这是一个简单的示例(只需将其放入viewDidLoad
要复制的新项目中)。将文本视图的字体大小更改为较小的(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;
所以我想我的问题是,即使文本需要多页,我如何才能恢复放大镜和编辑菜单功能?
非常感谢您的帮助!