多次重绘 NSBezierPath,并进行缩放?
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以首先删除该行:
以便您的路径不会绑定到视图中的特定位置。完成后,您可以调用方法来获取路径、移动到一个点、描边路径、移动到另一个点、描边路径等等。如果要从路径描述中的起点移动,请使用
-relativeMoveToPoint:
。You could start by removing the line:
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:
.看来这可能会成功,但我不确定这是否是最好的方法?
It appears this might do the trick, however I am not sure if it is the best way to do it?