Objective c 多次重绘NSBezierPath,并进行缩放?

Objective c 多次重绘NSBezierPath,并进行缩放?,objective-c,nsview,nsbezierpath,Objective C,Nsview,Nsbezierpath,我目前正在使用NSString助手方法在NSView上编写许多文本块。。。然而,在许多情况下,写大量重复的文本是非常缓慢的。我正在尝试重新编写代码,以便将文本转换为NSBezierPath,生成一次,然后绘制多次。以下内容将在屏幕底部绘制文本 我仍在努力阅读苹果的文档,以了解这是如何工作的,但与此同时,我想知道是否有一种简单的方法可以修改这段代码,从而在多个位置重新绘制路径 // Write a path to the view NSBezierPath* path = [self bezier

我目前正在使用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];
}

编辑:我正在尝试优化一个向屏幕输出大量文本序列(如10000个数字序列)的应用程序。每个数字周围都有标记和/或它们之间有不同的间距,有些数字上面、下面或之间有点和/或线。这与本文档第二页顶部的示例类似,但输出量要大得多。

这似乎可以解决问题,但我不确定这是否是最好的方法

// 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];

看起来这可能会奏效,但我不确定这是否是最好的方法

// 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];

您可以从删除该行开始:

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

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

可以从删除行开始:

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

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

为什么首先要绘制这么多次?看来
nsaffinettransform
可以用于永久更改坐标,但是如果要在多个不同的位置多次重新绘制,该怎么办,这就像我想要一个
[nsaffinettransforminitpathwithtranslation:oldpath offset:offset]
NB:最终的答案是使用较低级别的核心图形和核心文本框架重写我的所有代码。这样做意味着我可以在台式机和iOS设备上快速渲染视图。为什么一开始就要绘制这么多次?看来
NSAffineTransform
可以用来永久更改坐标,但是如果我想在多个不同的位置多次重新绘制,就像我想要一个
[NSAffineTransform initPathWithTranslation:旧路径偏移量:偏移量]
NB:最终的答案是使用较低级别的核心图形和核心文本框架重写我的所有代码。这样做意味着我可以在桌面和iOS设备上非常快速地呈现视图。听起来像是我想要的,但是我认为我知道的不足以实现您的建议。在
[path fill]
调用?即
NSPath*path=…;[path moveToPoint:NSMakePoint(0,7)];[NSColor grayColor]setFill];[path fill];
不绘制任何内容。听起来像是我想要的,但我认为我知道的不够多,无法实现您的建议。在
[path fill]之前,我在主函数中放了什么
call?ie
NSPath*path=…;[path moveToPoint:NSMakePoint(0,7)];[NSColor grayColor]setFill];[path fill];
不绘制任何内容。