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

发布于 2024-11-18 00:23:30 字数 2008 浏览 1 评论 0原文

我目前正在使用 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 个数字的序列)输出到屏幕的应用程序。每个数字周围都有标记和/或它们之间有不同大小的空间,并且某些数字的上方、下方或之间有点和/或线。它类似于本文档第二页顶部的示例,但输出更多。

I am currently writing many chunks of text on an NSView using the NSString helper method ... however it is very slow to write a large amount of in many cases repeated text. I am trying to re-write the code so that the text is converted to NSBezierPath's that are generated once, then drawn many times. The following will draw text at the bottom of the screen.

I am still trying to read through the apple documentation to understand how this works, but in the mean time I wonder if there is an easy way to alter this code to re-draw the path in multiple locations?

// Write a path to the view
NSBezierPath* path = [self bezierPathFromText: @"Hello world!" maxWidth: width];
[[NSColor grayColor] setFill];
[path fill];

Here is the method that writes some text into a path:

-(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];
}

Edit: I am attempting to optimise an application that outputs to screen, a sequence of large amounts text such as a sequence of 10,000 numbers. Each number has markings around it and/or differing amounts of space between them, and some numbers have dots and/or lines above, below or between them. Its like the example at the top of page two of this document but with much much more output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-11-25 00:23:30

您可以首先删除该行:

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

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

You could start by removing the line:

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

so that your path isn't tied to a particular location in the view. With that done, you can call your method to get the path, move to a point, stroke the path, move to another point, stroke the path, and so on. If you want to move from the starting point within the path description, use -relativeMoveToPoint:.

菩提树下叶撕阳。 2024-11-25 00:23:30

看来这可能会成功,但我不确定这是否是最好的方法?

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

It appears this might do the trick, however I am not sure if it is the best way to do it?

// 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文