通过缩放多次重绘NSBezierPath?

时间:2011-07-03 05:55:37

标签: objective-c nsview nsbezierpath

我目前正在使用NSString帮助程序方法在NSView上编写许多文本块...但是在很多情况下编写大量重复文本的速度非常慢。我试图重新编写代码,以便将文本转换为生成一次的NSBezierPath,然后多次绘制。以下内容将在屏幕底部绘制文字。

我仍在尝试阅读Apple文档以了解其工作原理,但同时我想知道是否有一种简单的方法可以更改此代码以在多个位置重新绘制路径?

// 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 个答案:

答案 0 :(得分:1)

您可以先删除该行:

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

这样您的路径就不会绑定到视图中的特定位置。完成后,您可以调用方法来获取路径,移动到某个点,描边路径,移动到另一个点,描边路径,等等。如果要从路径描述中的起点移动,请使用-relativeMoveToPoint:

答案 1 :(得分: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];