2013-02-14 38 views
2

我试图在我的OS X应用程序中实现打印,但存在一个问题,我无法解决,我希望也许你们中的一个人有了一个主意。更改页面方向不会缩放内容

在Safari中,您可以点击打印,打印面板将显示如何以纵向模式打印内容,并且如果您单击方向按钮,它会以横向模式显示内容。

Portrait mode

Landscape mode

注意的内容进行缩放定向的每一个变化。

如果我在我的应用程序中尝试相同的内容不会改变;其宽度在纵向和横向上保持不变。

NSPrintOperation或NSPrintInfo中是否有我可能忽略的设置?

更新 - 现在代码:

- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError { 
    NSPrintInfo *pInfo = [NSPrintInfo sharedPrintInfo]; 
    [pInfo setLeftMargin:32]; 
    [pInfo setRightMargin:32]; 
    [pInfo setTopMargin:64]; 
    [pInfo setBottomMargin:64]; 
    [pInfo setHorizontalPagination:NSFitPagination]; 
    [pInfo setVerticallyCentered:NO]; 
    [[pInfo dictionary] setValue:[NSNumber numberWithBool:YES] forKey:NSPrintHeaderAndFooter]; 
    [[pInfo dictionary] addEntriesFromDictionary:printSettings]; 

    PrintTextView *printView = [[[PrintTextView alloc] initWithFrame:[pInfo imageablePageBounds]] autorelease]; 
    printView.printJobTitle = @"Printed"; 

    MSTablePrint *tPrint = [[[MSTablePrint alloc] init] autorelease]; 
    NSMutableArray *itemArray = [[[NSMutableArray alloc] init] autorelease]; 

    /* 
     Objects are added to "itemArray" 
    */ 

    NSAttributedString *atString = [tPrint attributedStringFromItems:itemArray]; 
    [[printView textStorage] setAttributedString:atString]; 

    NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:printView printInfo:pInfo]; 

    return printOp; 
} 

- (IBAction)print:(id)sender { 
    NSError *printError = nil; 
    NSPrintOperation *printOperation = [self printOperationWithSettings:nil error:&printError]; 
    [[printOperation printPanel] setOptions:[[printOperation printPanel] options] | NSPrintPanelShowsPageSetupAccessory]; 

    [printOperation runOperation]; 
} 

非常感谢您的帮助, 最良好的祝愿, UEM

+0

包含您当前打印操作的代码。 NSPrintOperation或NSPrintInfo有很多设置。 – 2013-02-14 15:36:46

+0

Thx看着它。我包含了代码。 – uem 2013-02-14 16:08:27

回答

3

你不能完成你的NSPrintOperation设置或NSPrintInfo布局变化设置。您将需要创建一个继承的视图。在这个视图中他们有两种方法(adjustPageHeightNew,adjustPageWidthNew)。当您需要更改视图布局时,会通知这些方法。确保你也叫超级方法。

在该方法中,更改视图的布局以及所有子视图。

- (void)adjustPageHeightNew:(CGFloat *)newBottom top:(CGFloat)oldTop bottom:(CGFloat)oldBottom limit:(CGFloat)bottomLimit 
{ 
    [super adjustPageHeightNew:newBottom top:oldTop bottom:oldBottom limit:bottomLimit]; 
    { 
     NSPrintOperation *printOperation = [NSPrintOperation currentOperation]; 

     if(printOperation) 
     { 
      NSPrintInfo *printInfo = [printOperation printInfo]; 
      CGFloat pageMargineX = ([printInfo leftMargin] + [printInfo rightMargin]); 
      CGFloat pageMargineY = ([printInfo topMargin] + [printInfo bottomMargin]); 
      CGFloat pageWidth = ([printInfo paperSize].width - pageMargineX); 
      CGFloat pageHeight = ([printInfo paperSize].height - pageMargineY); 

      [self setFrame:NSMakeRect(0, 0, pageWidth, ([[self subviews] count] * pageHeight))]; 

      for(NSUInteger nView = 0; nView < [[self subviews] count]; nView ++) 
      { 
       NSTextView *PagedView = [[self subviews] objectAtIndex:nView]; 

       [PagedView setFrame:NSMakeRect(0, (nView * pageHeight), pageWidth, pageHeight)]; 
      } 
     } 
    } 
} 
+0

非常感谢您指点我正确的方向。我在我的NSTextView子类中实现** adjustPageHeightNew **和** adjustPageWidthNew **,但只有** adjustPageHeightNew **被调用。我在** adjustPageWidthNew **中放置了一个断点,但Xcode只是不会停在那里。你有什么想法,为什么这样? – uem 2013-05-29 09:56:45