How do I make an NSLayoutManager that assigns each paragraph in its text to a different 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.

有帮助吗?

解决方案

I think the easiest way to do this is to subclass NSTextContainer and override

- (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. 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. This does mean you'll have to track paragraph attributes and the text containers they correlate to (and vice versa).

其他提示

Another way is inserting page break control character (ASCII code: 12) at the end of each paragraph.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top