Reputation: 831
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?
Upvotes: 1
Views: 916
Reputation: 1998
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
}
Upvotes: 0
Reputation: 831
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
}
Upvotes: 1