简体   繁体   English

每个段落带有NSTextContainer的NSLayoutManager

[英]NSLayoutManager with an NSTextContainer per paragraph

How do I make an NSLayoutManager that assigns each paragraph in its text to a different NSTextContainer? 如何创建一个NSLayoutManager,将其文本中的每个段落分配给不同的NSTextContainer?

This would be similar to the multiple page/column usage that is common in TextKit, but each page corresponds to a different paragraph of variable length. 这类似于TextKit中常见的多页/列使用,但每个页面对应于可变长度的不同段落。

I think the easiest way to do this is to subclass NSTextContainer and override 我认为最简单的方法是子类化NSTextContainer并覆盖

- (CGRect)lineFragmentRectForProposedRect:(CGRect)proposedRect atIndex:(NSUInteger)characterIndex writingDirection:(NSWritingDirection)baseWritingDirection remainingRect:(CGRect *)remainingRect

In your custom method, you'd inspect the attributes at characterIndex and see if they matched the paragraph that text container is assigned to. 在自定义方法中,您将检查characterIndex处的属性,并查看它们是否与分配了文本容器的段落相匹配。 If they are, just return super 's implementation of the method, otherwise return CGRectZero to signify that the proposed rect is invalid for this text container. 如果是,则返回super的方法实现,否则返回CGRectZero以表示建议的rect对此文本容器无效。 This does mean you'll have to track paragraph attributes and the text containers they correlate to (and vice versa). 这意味着你必须跟踪段落属性和它们相关的文本容器(反之亦然)。

另一种方法是在每个段落的末尾插入分页符控制字符(ASCII代码:12)。

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

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