2013-02-14 72 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