可可.创建NSTextView,向其中添加NSString并打印 [英] Cocoa. Create NSTextView, add an NSString to it and print
问题描述
我有一个带有用于打印收据的按钮的窗口.我需要做的是创建一个简单的NSTextView,向其中添加一个NSString(至少类似于"Hello World"之类的东西)并打印它而不在窗口上显示NSTextView.
I have a window with a button for printing receipts. What I need to do is to create a simple NSTextView, add an NSString to it (at least something like "Hello World") and print it without displaying NSTextView on the window.
这是我目前拥有的:
NSTextView *textView = [[NSTextView alloc] init];
NSString *text = @"testing";
[textView setEditable:true];
NSRange range = NSMakeRange( 0, [[textView string] length]);
[textView setSelectedRange:range];
[[[textView textStorage] mutableString] appendString:text];
NSPrintOperation *printOperation;
printOperation = [NSPrintOperation printOperationWithView:textView];
[printOperation runOperation];
当我运行它时,我看到打印对话框,但是预览为空. 当我更改printOperationWithView:textView];从textView到我窗口上的现有视图之一,它可以正常打印.
when I run it, I see printing dialog, but preview is empty. When I change printOperationWithView:textView]; from textView to one of the existing views on my window, it prints ok.
最主要的是..我不想在单击打印"按钮后显示视图.理想情况下,我想打印文本,而不是视图.
The main thing is.. I don't want to display the view after I click Print button. Ideally I would like to print the text, not the view.
推荐答案
您是否尝试过将-initWithFrame
与非零矩形一起使用?
Have you tried using -initWithFrame
with a nonzero rect?
NSTextView *textview = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 468, 648);
(468 x 648使其适合具有1英寸页边距的A4纸.)
(468 x 648 would make it fit an A4 sheet with 1-inch margins.)
这篇关于可可.创建NSTextView,向其中添加NSString并打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!