Question

I create a paged UIScrollView and fill its contents with text, using TextKit. All the paging works perfectly. What has me stumped is that when all of the text fits into one page, everything works as expected. However, if the text is longer and requires the scrollview to create multiple pages, the text is no longer highlightable/selectable.

Here is a simple example (just plop this into a new project's viewDidLoad to replicate). Changing the font size of the text views to something small (12pt) so that all the text fits onto one page will result in expected behavior. Conversely, a larger size (22pt) will create multiple pages for the scrollview and we lose the intractability of the text.

// 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;

So I guess my question is, how can I get the Magnifying Glass and Edit Menu functionality back even when the text requires multiple pages?

Thanks a lot for any help!

Was it helpful?

Solution

This has nothing to do with the scroll view. A text kit stack where the text "flows" from one text view to another (one layout manager, multiple text containers) is not editable. That's just how Text Kit works.

You can detect that the user is holding a finger on a word, of course, but you will have to detect that. You can query the layout manager to help you. You can paint a highlight at that place, make a menu appear, etc. But you have to do that; the text view is not going to do it for you.

(Note also that UIWebView is now pageable. For some purposes, a paging web view is going to be simplest.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top