How do I get "Page Attributes" option in Cocoa print dialog? - cocoa

The program I'm writing runs under OS X 10.5 Leopard. My target has its Base SDK and Deployment Target both set to Mac OS X 10.5. When I initiate printing, my print dialog doesn't show the Page Attributes option in which the user can select page size and orientation.
Other programs running under Leopard do show this option:
Here's the code that initiates printing:
-(void)print {
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
TemperaturePressurePrintView *printView = [[TemperaturePressurePrintView alloc] initWithFrequencies:frequencies];
if (printView) {
[[NSPrintOperation printOperationWithView:printView printInfo:printInfo] runOperation];
[printView release];
}
}
What do I need to do to get Page Attributes to show up in my print dialog?

This was a tough thing to search for because the results were mostly about using the print panel, not programming one. I finally found a clue on Cocoabuilder where it mentions NSPrintPanelOptions and NSPrintPanel's -setOptions: method.
This code accomplishes what I need:
-(void)print {
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
TemperaturePressurePrintView *printView = [[TemperaturePressurePrintView alloc] initWithFrequencies:frequencies];
if (printView) {
NSPrintOperation *op = [NSPrintOperation printOperationWithView:printView printInfo:printInfo];
[[op printPanel] setOptions:[[op printPanel] options] | NSPrintPanelShowsPageSetupAccessory];
[op runOperation];
[printView release];
}
}

It's a few years after the original answer and macOS Sierra seems to have introduced a bug into the behaviour of panels that have the 'NSPrintPanelShowsPageSetupAccessory' option set. Invalid values, such as a ridiculously large scale, cause crashes instead of displaying an alert sheet.
Fortunately there is a workaround. Using
NSPrintPanelShowsPaperSize | NSPrintPanelShowsOrientation | NSPrintPanelShowsScaling
instead seems to result in a panel that works fine.

Related

tvOS 14.3 UISearchController: How to locked in display

Short version, is there a way to make this 14.3 look
into this prior to 14.3
Long version,
In every OS iteration the look of the UISearchController is changing. Before it only change the keyboard portion but with 14.3 it changed that the keyboard portion is on left side while the search results are on the right, which is what we don't like since we have custom view and overlays on top of it.
Any APIs to make it revert to the previous iteration, that is the keyboard are all within one horizontal line and search results on bottom, and stay that way forever?
Here's the code for the integration. The 14.3 UI look did mess up the app overall.
_searchResults = [[UITableViewController alloc] init];
_searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResults];
_searchController.searchResultsUpdater = self;
_searchController.view.backgroundColor = [UIColor clearColor];
_searchController.searchBar.keyboardAppearance = UIKeyboardAppearanceDark;
_searchController.searchBar.placeholder = #"TV Shows, Movies, Keywords";
_searchController.obscuresBackgroundDuringPresentation = false;
_searchController.hidesNavigationBarDuringPresentation = true;
_searchContainer = [[UISearchContainerViewController alloc] initWithSearchController:_searchController];
_navController = [[UINavigationController alloc] initWithRootViewController:_searchContainer];
[_navController willMoveToParentViewController:self];
[self addChildViewController:_navController];
[self.view addSubview:_navController.view];
There is no code to change keyboard looking but it's simply changed on your device setting:
Settings>General>Keyboard = Auto
but you can changed to "Linear" or "Grid"
for more information see the video:
Discover search suggestions for Apple TV

Cocoa osx PDFView NSPrintOperation PrintPanel not showing page preview

In my app for Mac I have a webview that shows some html content. I create a PDFDocument from that webview and then I want to print that document. So I create a PDFView from the document and then I call the NSPrintOperation printOperationWithView. When showing the print panel all appears correct except the page preview which appears blank, but if I press the details button the panel is refreshed and the page preview appears correctly.
How can I solve this??
Need help please. Thanks in advance.
This is an example of my problem:
1- The print panel shows with a blank page preview. Next I press show details.
2- After refreshing the panel the page preview appears correctly.
This is my code:
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setTopMargin:0.0];
[printInfo setBottomMargin:0.0];
[printInfo setLeftMargin:0.0];
[printInfo setRightMargin:0.0];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setVerticalPagination:NSAutoPagination];
[printInfo setVerticallyCentered:NO];
[printInfo setHorizontallyCentered:YES];
NSData *pdfFinal = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];
PDFDocument *doc = [[PDFDocument alloc] initWithData:pdfFinal];
PDFView *pdfView = [[PDFView alloc] init];
[pdfView setDocument:doc];
NSPrintOperation *op;
op = [NSPrintOperation printOperationWithView:pdfView.documentView printInfo:printInfo];
[op setShowsProgressPanel:YES];
[op setShowsPrintPanel:YES];
[op runOperation];
From this link:
PDFDocument *doc = ...;
// Invoke private method.
// NOTE: Use NSInvocation because one argument is a BOOL type. Alternately, you could declare the method in a category and just call it.
BOOL autoRotate = NO; // Set accordingly.
NSMethodSignature *signature = [PDFDocument instanceMethodSignatureForSelector:#selector(getPrintOperationForPrintInfo:autoRotate:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:#selector(getPrintOperationForPrintInfo:autoRotate:)];
[invocation setArgument:&printInfo atIndex:2];
[invocation setArgument:&autoRotate atIndex:3];
[invocation invokeWithTarget:doc];
// Grab the returned print operation.
void *result;
[invocation getReturnValue:&result];
NSPrintOperation *op = (__bridge NSPrintOperation *)result;
[op setShowsPrintPanel:YES];
[op setShowsProgressPanel:YES];
[op runOperation];
This works on OSX from 10.4 to 10.10 (Yosemite).
EDIT: You can also see this answer which is similar, but with less lines of code.
At a guess, since PDFView is a subclass of NSView, whose designated initializer is -initWithFrame:, not -init, your call to PDFView *pdfView = [[PDFView alloc] init] may not be allowing the PDFView to set up its initial state, though subsequent calls to its machinery may be magically resolving this state for you but NSView and subclasses tend to behave strangely (particularly with respect to drawing) when you don't use the proper designated initializer (which means its frame and bounds are equal to NSZeroRect).
Try using -initWithFrame: with some reasonable, non-zero rectangle.
Update
Alright, just a wild shot in the dark, but the documentation for NSPrintOperation says that -runOperation blocks the main thread and recommends using -runOperationModalForWindow:delegate:didRunSelector:contextInfo: to avoid blocking the main thread entirely. Is it possible that by blocking the main thread something else is being prevented from doing its initial work (I'd call this either undocumented behavior or an API bug, but...)? Try implementing the modal version instead and see if that helps. If not, I'd actually file a bug report with Apple.

UITextView setText should not jump to top in ios8

Following iOS 8 code is called every second:
- (void)appendString(NSString *)newString toTextView:(UITextView *)textView {
textView.scrollEnabled = NO;
textView.text = [NSString stringWithFormat:#"%#%#%#", textView.text, newString, #"\n"];
textView.scrollEnabled = YES;
[textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
}
The goal is to have the same scrolling down behaviour as the XCode console when the text starts running off the bottom. Unfortunately, setText causes the view to reset to the top before I can scroll down again with scrollRangeToVisible.
This was solved in iOS7 with the above code and it worked, but after upgrading last week to iOS8, that solution no longer seems to work anymore.
I can't figure out how to get this going fluently without the jumping behaviour?
I meet this problem too. You can try this.
textView.layoutManager.allowsNonContiguousLayout = NO;
refrence:http://hayatomo.com/2014/09/26/1307
The following two solutions don't work for me on iOS 8.0.
textView.scrollEnabled = NO;
[textView.setText: text];
textView.scrollEnabled = YES;
and
CGPoint offset = textView.contentOffset;
[textView.setText: text];
[textView setContentOffset:offset];
I setup a delegate to the textview to monitor the scroll event, and noticed that after my operation to restore the offset, the offset is reset to 0 again. So I instead use the main operation queue to make sure my restore operation happens after the "reset to 0" option.
Here's my solution that works for iOS 8.0.
CGPoint offset = self.textView.contentOffset;
self.textView.attributedText = replace;
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{
[self.textView setContentOffset: offset];
}];
Try just to add text to UITextView (without scrollRangeToVisible/scrollEnabled). It seams that hack with scroll enabled/disabled is no more needed in iOS8 SDK. UITextView scrolls automatically.

How to know whether a window is minimised or not?

I'd like to know which windows are visible on screen. CGWindowListCopyWindowInfo gives me the list of windows, which is great except it also lists minimised windows as well.
I tried to use kCGWindowIsOnscreen to detect hidden/minimised windows but it always give TRUE for all windows. Is there any way to detect that somehow?
- (void) checkWindows {
NSMutableArray *windows = (__bridge NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
for (NSDictionary *window in windows) {
NSString *owner = [window objectForKey:#"kCGWindowOwnerName" ];
NSString *name = [window objectForKey:#"kCGWindowName" ];
CFBooleanRef visibleBR = (CFBooleanRef)[window objectForKey:#"kCGWindowIsOnscreen"];
bool visible = CFBooleanGetValue(visibleBR);
NSLog(#"%#,%#,Visible:%#",owner,name,visible?#"YES":#"NO");
}
}
UPDATE: very strange, it's only Microsoft word. In fact it doesn't have to be hidden, Word creates a full screen window which is not visible but listed among the visible windows.
The issue was caused by MS Word.
It creates a full screen window which is not visible but listed among the visible windows. CGWindowListCopyWindowInfo lists visible/minimised windows correctly.

Provide print option from mac app

I have a Mac app. In my Mac app one of my screen has a scrollView which contains a text field. On the same screen I have a button that needs to provide a print option. The text of the text field can be printed. Print button should call the Mac OS X print dialog box. I am able to open the print dialog box by connecting the button to the print option of the first responder through xib but when I preview I don't see any text except the print button. Please help.
check out the NSPrintOperation Class Reference.
NSPrintOperation Class Reference
you will probably need to compose the text to a NSView that is large enough to fit your scroll view contents... I haven't ever had to print from a scrollView, so i don't know.
look at
+ printOperationWithView:
you will probably have to override the print: action, remember that you will be sending that to the first responder... and should fall through to your NSDocument or NSApplication subclass, but I would probably try to grab it at NSDocument if document based, NSWindow (subclass or delegate) if not.
I got the answer for this.I am using the below code,
- (void)print:(id)sender {
// page settings for printing
[self setPrintInfo:[NSPrintInfo sharedPrintInfo]];
[printInfo setVerticalPagination:NSAutoPagination];
float horizontalMargin, verticalMargin;
horizontalMargin = 0;
verticalMargin = -100;
[printInfo setLeftMargin:horizontalMargin];
[printInfo setRightMargin:horizontalMargin];
[printInfo setHorizontallyCentered:YES];
[printInfo setTopMargin:-600];
[printInfo setBottomMargin:verticalMargin];
[[NSPrintOperation printOperationWithView:sampleText] runOperation];
}

Resources