1

我创建了一个分页 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;

所以我想我的问题是,即使文本需要多页,我如何才能恢复放大镜和编辑菜单功能?

非常感谢您的帮助!

4

1 回答 1

2

这与滚动视图无关。文本从一个文本视图“流动”到另一个(一个布局管理器、多个文本容器)的文本工具包堆栈是不可编辑的。这就是 Text Kit 的工作原理。

当然,您可以检测到用户正在用手指按住单词,但必须检测到这一点。您可以查询布局管理器来帮助您。你可以在那个地方画一个高光,让一个菜单出现,等等。但必须这样做;文本视图不会为你做这件事。

(还要注意 UIWebView 现在是可分页的。出于某些目的,分页 Web 视图将是最简单的。)

于 2014-03-12T02:02:03.080 回答