1

私は現在、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 ページ目の上部にある例に似ていますが、はるかに多くの出力があります。

4

2 に答える 2

1

次の行を削除することから始めることができます。

[path moveToPoint: NSMakePoint(0, 7)];

パスがビュー内の特定の場所に結び付けられないようにします。これが完了すると、メソッドを呼び出して、パスを取得し、ポイントに移動し、パスをストロークし、別のポイントに移動し、パスをストロークすることができます。パス記述内の開始点から移動する場合は、 を使用します-relativeMoveToPoint:

于 2011-07-03T06:28:54.900 に答える
0

これでうまくいくようですが、それが最善の方法であるかどうかはわかりません。

// Create the path
NSBezierPath* path = [self bezierPathFromText: @"Fish are fun to watch in a fish tank, but not fun to eat, or something like that." maxWidth: width];

// Draw a copy of it at a transformed (moved) location
NSAffineTransform* transform = [[NSAffineTransform alloc] init];
[transform translateXBy: 10 yBy: 10];
NSBezierPath* path2 = [path copy];
[path2 transformUsingAffineTransform: transform];
[[NSColor greenColor] setFill];
[path2 fill];
[path2 release];
[transform release];
[path2 release];

// Draw another copy of it at a transformed (moved) location
transform = [[NSAffineTransform alloc] init];
[transform translateXBy: 10 yBy: 40];
path2 = [path copy];
[path2 transformUsingAffineTransform: transform];
[[NSColor greenColor] setFill];
[path2 fill];
[path2 release];
[transform release];
于 2011-07-03T06:28:51.927 に答える