如何使用 Cocoa 在 PDF 中生成网格?
我是 Cocoa 和 Objective-C 的初学者。
我想制作一个 Cocoa 应用程序,它将生成一个网格网格(用于练习中国书法)并导出为 PDF,类似于此在线生成器: http://incompetech.com/graphpaper/chinesequarter/。
我应该如何生成网格?我尝试将 Quartz 与 CustomView 一起使用,但没有取得很大进展。另外,一旦在 CustomView 中绘制了网格,将其“打印”到 PDF 的方法是什么?
感谢您的帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现一个自定义视图来绘制它。
这是一种方法; AppKit绘图是另一个。不过,它们的大部分部分非常相似。 AppKit直接基于PostScript,而Quartz则间接基于PostScript。
您应该针对您的问题提出更具体的问题。
发送一条
dataWithPDFInsideRect:
消息,传递其 边界。请注意,没有“一旦在 CustomView 中绘制网格”。尽管可能存在一些内部缓存,但从概念上讲,视图不会绘制一次并保留它;而是绘制一次。它会在需要的时候、每次需要的时候、到需要的地方。当窗口需要重新绘制时,Cocoa 将告诉脏区域中的任何视图进行(重新)绘制,并且它们最终将绘制到屏幕上。当您请求 PDF 数据时,这也会告诉视图进行绘制,并且它将绘制到记录 PDF 数据的上下文中。这使得视图既可以是惰性的(仅在需要时绘制),又可以在不同的上下文中以不同的方式绘制(例如,在打印时)。
Implement a custom view that draws it.
That's one way; AppKit drawing is the other. Most parts of them are very similar, though; AppKit is directly based on PostScript, while Quartz is indirectly based on PostScript.
You should ask a more specific question about your problem.
Send it a
dataWithPDFInsideRect:
message, passing its bounds.Note that there is no “once the grid is drawn in the CustomView”. Though there may be some internal caching, conceptually, a view does not draw once and hold onto it; it draws when needed, every time it's needed, into where it's needed. When the window needs to be redrawn, Cocoa will tell any views that are in the dirty area to (re)draw, and they will draw ultimately to the screen. When you ask for PDF data, that will also tell the view to draw, and it will draw into a context that records PDF data. This allows the view both to be lazy (draw only when needed) and to draw differently in different contexts (e.g., when printing).
哎呀,你问的是 Cocoa,这是 Cocoa Touch,但我将把它留在这里,因为它可能会有一些用处(至少对于后来发现它的其他人来说)。
您可以在视图中绘制内容,然后将其中的内容放入 pdf 中。
此代码将获取 UIView(此处称为sheetView)中绘制的内容,将其放入 pdf 中,然后将其作为附件放入电子邮件中(以便您现在可以看到它)。您需要在标头中引用协议 MFMailComposeViewControllerDelegate。
你还需要这个方法...
Oops, you were asking about Cocoa and this is Cocoa Touch, but I'll leave it here as it may be some use (at least to others who find this later).
You can draw things in the view and then put what's there into a pdf.
This code will take what's drawn in a UIView (called sheetView here), put it into a pdf, then put that as an attachment in an email (so you can see it for now). You'll need to reference the protocol MFMailComposeViewControllerDelegate in your header.
You'll also need this method...