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.
Related
I need to know if the PDFDocument of a PDFView has been printed successfully in order to do some housekeeping afterwards.
When printing a regular NSView from NSDocument, I can do
NSPrintOperation *op = [NSPrintOperation
printOperationWithView:myRegularPrintView
printInfo:self.printInfo];
[op setCanSpawnSeparateThread:NO]; // Because we want to clean up afterwards
[op setShowsPrintPanel:YES];
[self runModalPrintOperation:op
delegate:self
didRunSelector:#selector(documentDidRunModalPrintOperation:success:contextInfo:)
contextInfo:NULL];
In the documentDidRunModalPrintOperation callback I can do the housekeeping. But printing the content of a PDFView only works correctly if I call
[myPDFView printWithInfo:[NSPrintInfo sharedPrintInfo] autoRotate:YES];
So I see no way to run the print operation with a callback function that is going to be called when the print panel is closed.
Since macOS 10.7 there is a function in PDFDocument which returns a NSPrintOperation, so one can simply do
NSPrintOperation *op = [myPDFView.document printOperationForPrintInfo:self.printInfo scalingMode:kPDFPrintPageScaleToFit autoRotate:YES];
and then continue just like with a normal NSView and add a callback when calling runModalPrintOperation.
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
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.
I have a basic NSRecessedBezelStyle NSButton added via IB to an NSView. Why is the font messed up in its unselected state? Is this normal?
As you can see, when pushed the recessed button looks fine, but unpressed it's solid black with no shadow. Am I missing something really obvious somewhere? I tried messing around with setAttributedTitle and setAttributedAlternateTitle but that yielded odd results with the push on push off mechanic.
That is the expected behavior for NSRecessedBezelStyle with the default "Push On Push Off" Type, bezeled in On state, plain text in OFF, additionally you can change the Type so the bezel is only displayed when hovering, here is the code to make it gray.
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
[attrsDictionary setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName];
[attrsDictionary setObject:[NSFont boldSystemFontOfSize:12.0] forKey:NSFontAttributeName];
NSMutableParagraphStyle *paragraph = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paragraph setAlignment:NSCenterTextAlignment];
[attrsDictionary setObject:paragraph forKey:NSParagraphStyleAttributeName];
NSAttributedString *str = [[[NSAttributedString alloc] initWithString:#"Button" attributes:attrsDictionary] autorelease];
[button setAttributedTitle:str];
I am using the following to create an NSPopupButton programmatically:
...
NSPopUpButton *accessoryView = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 24) pullsDown:YES];
NSFont *aFont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
[accessoryView setBezelStyle:NSRecessedBezelStyle];
[accessoryView setFont:aFont];
[accessoryView setShowsBorderOnlyWhileMouseInside:YES];
[accessoryView setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
NSArray *popupItems = [[NSArray alloc] initWithObjects:#"Your Account", #"Sign In", #"Create Account", nil];
[accessoryView addItemsWithTitles:popupItems];
...
Now, when I add the NSPopUpButton to my view, I end up with the button's text overlapping the icon used for the dropdown menu. I have seen this previously when I use NSControl:setAlignment but I am not using this here. Here is the output:
Can anyone see what I'm doing wrong?
Take care,
Jeremy
It just so happens this is a simple fix. (Thanks Beelsebob on irc.freenode.net!) Basically, you need to have the following code:
[[accessoryView cell] setArrowPosition:NSPopUpArrowAtBottom];
in there somewhere. (I added it just below the line to add the menu items.) I had read the API docs on this call a few times before, since I had implemented the same call with a value of NSPopUpNoArrow to remove the arrow as an interim fix, but it didn't make it clear that the proper value being used above would do what it's doing. Problem solved.