私は現在、NSStringヘルパーメソッドを使用してNSViewに多くのテキストのチャンクを書いています...しかし、多くの場合、繰り返されるテキストを大量に書き込むのは非常に遅くなります。テキストがNSBezierPathに変換され、一度生成されてから何度も描画されるように、コードを書き直そうとしています。以下は、画面の下部にテキストを描画します。
これがどのように機能するかを理解するためにアップルのドキュメントを読み通そうとしていますが、当面の間、このコードを変更して複数の場所でパスを再描画する簡単な方法があるのではないかと思いますか?
// Write a path to the view
NSBezierPath* path = [self bezierPathFromText: @"Hello world!" maxWidth: width];
[[NSColor grayColor] setFill];
[path fill];
テキストをパスに書き込むメソッドは次のとおりです。
-(NSBezierPath*) bezierPathFromText: (NSString*) text maxWidth: (float) maxWidth {
// Create a container describing the shape of the text area,
// for testing done use the whole width of the NSView.
NSTextContainer* container = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(maxWidth - maxWidth/4, 60)];
// Create a storage object to hold an attributed version of the string to display
NSFont* font = [NSFont fontWithName:@"Helvetica" size: 26];
NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, nil];
NSTextStorage* storage = [[NSTextStorage alloc] initWithString: text attributes: attr];
// Create a layout manager responsible for writing the text to the NSView
NSLayoutManager* layoutManger = [[NSLayoutManager alloc] init];
[layoutManger addTextContainer: container];
[layoutManger setTextStorage: storage];
NSRange glyphRange = [layoutManger glyphRangeForTextContainer: container];
NSGlyph glyphArray[glyphRange.length];
NSUInteger glyphCount = [layoutManger getGlyphs:glyphArray range:glyphRange];
NSBezierPath* path = [[NSBezierPath alloc] init];
//NSBezierPath *path = [NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, 30, 30)];
[path moveToPoint: NSMakePoint(0, 7)];
[path appendBezierPathWithGlyphs:glyphArray count: glyphCount inFont:font];
// Deallocate unused objects
[layoutManger release];
[storage release];
[container release];
return [path autorelease];
}
編集: 10,000 の数字のシーケンスなど、大量のテキストのシーケンスを画面に出力するアプリケーションを最適化しようとしています。各数字には、その周りにマーキングがあり、および/またはそれらの間に異なる量のスペースがあり、一部の数字には、それらの上、下、または間にドットおよび/または線があります. このドキュメントの 2 ページ目の上部にある例に似ていますが、はるかに多くの出力があります。