简体   繁体   English

iOS 7文本工具包:NSLayoutManager何时填充了最后一个NSTextContainer?

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

I'm using Text Kit in combination with UIPageViewController to create a book. 我将Text Kit与UIPageViewController结合使用来创建一本书。 The original text is stored in a text file (.html) and placed into a NSTextStorage. 原始文本存储在文本文件(.html)中,并放置在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. [NSLayoutManager addTextContainer:(NSTextContaner *)txt]方法效果很好,但是没有办法知道NSLayoutManager何时填充了最后一个NSTextContainer-即使显示所有下一个,它也只会返回NSTextContainer。 As result you get an all bank pages after the NSLayoutManager is done. 结果,完成NSLayoutManager后,您将获得所有库页面。

I've tried using the [NSLayoutManagerDelegate didCompleteLayoutForTextContainer: atEnd:] callback method but it isn't working properly. 我试过使用[NSLayoutManagerDelegate didCompleteLayoutForTextContainer:atEnd:]回调方法,但无法正常工作。 It returns atEnd flag = YES after filling each NSTextContainer, not just when the last NSTextContainer is filled. 填充每个NSTextContainer之后,它不仅返回最后一个NSTextContainer填充时间,还返回atEnd标志= YES。 I have set UITextView.scrollable = NO (suggested elsewhere) but that doesn't help. 我设置了UITextView.scrollable = NO(在其他地方建议),但这没有帮助。

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. 我也尝试通过在没有文本显示时调用UITextView.text来检查文本,但是该方法始终返回位于NSTextContainer / NSLayoutManager后面的整个NSTextStorage的内容。

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? 有没有一种方法可以测试UITextView来查看其是否为空或NSContainer或NSLayoutManager来查看完成的文本布局?

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.. 我一直无法正确调用call方法-我认为这是一个错误-但我找到了一种检查下一个容器是否为空的方法。

    [_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. 通过调用glyphRangeForTextContainer方法检查容器是否为最后一个。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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