简体   繁体   中英

iOS 7 Text Kit: When has NSLayoutManager filled its last NSTextContainer?

I'm using Text Kit in combination with UIPageViewController to create a book. The original text is stored in a text file (.html) and placed into a NSTextStorage.

The [NSLayoutManager addTextContainer:(NSTextContaner*)txt] method works great, but there is no way to know when the NSLayoutManager has filled the last NSTextContainer - it just keep returning NSTextContainer even after all the next is displayed. As result you get an all bank pages after the NSLayoutManager is done.

I've tried using the [NSLayoutManagerDelegate didCompleteLayoutForTextContainer: atEnd:] callback method but it isn't working properly. It returns atEnd flag = YES after filling each NSTextContainer, not just when the last NSTextContainer is filled. I have set UITextView.scrollable = NO (suggested elsewhere) but that doesn't help.

I also tried to check the text by calling UITextView.text when no text is displayed, but that method always return the contents of the entire NSTextStorage that lays behind the NSTextContainer/NSLayoutManager.

If I can't tell when the last container is filled I don't know when all the pages are laid out. Is there a way to test the UITextView to see if its empty or NSContainer, or NSLayoutManager to see done laying out text?

I was never able to get the call back method be called correctly - I think its a bug - but I found a way to check if the next container is going to be empty or not..

    [_layoutManager addTextContainer:textContainer];
    NSRange range = [_layoutManager glyphRangeForTextContainer:textContainer];
    if (range.length == 0) {
        return nil;// top adding container because this last one is empty
    }

Check whether container is last by calling glyphRangeForTextContainer method. If it exceeds number of glyphs, that's the last container.

layoutManager.addTextContainer(container)

let glyphRange = NSMaxRange((layoutManager.glyphRangeForTextContainer(container)))
if glyphRange >= layoutManager.numberOfGlyphs {
    // last container
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM