この個別のテキスト コンテナーを機能させるには、文字列の各部分の描画サイズを計算し、NSTextView をそのサイズに制限します。
NSLayoutManager * layout = [[NSLayoutManager alloc] init];
NSString * storedString = @"A\nquick\nBrown\nFox";
NSTextStorage * storage = [[NSTextStorage alloc] initWithString:storedString];
[storage addLayoutManager:layout];
//I assume you have a parent view to add the text views
NSView * view;
//Assuming you want to split up into separate view by line break
NSArray * paragraphs = [storedString componentsSeparatedByString:@"\n"];
for (NSString * paragraph in paragraphs)
{
NSSize paragraphSize = [paragraph sizeWithAttributes:@{}];
//Create a text container only big enough for the string to be displayed by the text view
NSTextContainer * paragraphContainer = [[NSTextContainer alloc] initWithContainerSize:paragraphSize];
[layout addTextContainer:paragraphContainer];
//Use autolayout or calculate size/placement as you go along
NSRect lazyRectWithoutSizeOrPlacement = NSMakeRect(0, 0, 0, 0);
NSTextView * textView = [[NSTextView alloc] initWithFrame:lazyRectWithoutSizeOrPlacement
textContainer:paragraphContainer];
[view addSubview:textView];
}
NSLayoutManager にデリゲートを追加して、テキスト コンテナーの使用状況を監視できます。
- (void)layoutManager:(NSLayoutManager *)aLayoutManager
didCompleteLayoutForTextContainer:(NSTextContainer *)aTextContainer
atEnd:(BOOL)flag
{
if (aTextContainer == nil)
{
//All text was unable to be displayed in existing containers. A new NSTextContainer is needed.
}
}