Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

phjo

macrumors regular
Original poster
Jan 8, 2008
149
1
Hi,

I have a problem when printing or exporting to pdf a view :

Suppose you have a window containing just a view, let's say a myView which inherits of course from NSView.

Suppose the drawRect: method is this :

Code:
- (void)drawRect:(NSRect)rect {
  CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  CGContextSetRGBFillColor(context,0,0,1,1);
  CGContextSelectFont(context,"Helvetica",2,kCGEncodingMacRoman);
  CGContextShowTextAtPoint(context,20,20,"test",4);
  CGContextBeginPath(context);
  CGContextMoveToPoint(context, 20,18);
  CGContextAddLineToPoint(context, 65,18);
  CGContextStrokePath(context);
  CGContextFlush(context);
}

Then you get a simple window which, when you select Print from the Edit menu and Preview will give you a representation of your window, with the window border and title (and weird titlebar corners in my experience), and the view inside it.

So far so good...

Suppose you now add this to the myView method :

Code:
-(BOOL)acceptsFirstResponder
{
	return YES;
}

then the Print command won't print the border of your window, nor its title, but only the content of the view inside the window which sounds reasonable enough.

But, it appears that although the text is still there, precisely where it should be, you have too look hard to see it, as it appears in a microscopic size... The lines, rect or whatever objects you drew on the view do appear in the same size as previously...

I tested on my macbook (10.4.11, xcode 2.4) and mac pro (10.5.2, xcode 3.0), both exhibited the same behavior...

As usual, I wonder... Am I doing something wrong ? and whatever the answer to this first question, how do I fix this ?

phjo
 

phjo

macrumors regular
Original poster
Jan 8, 2008
149
1
That sounds weird, as I couldn't see anything in NSView class documentation about that... Nothing in AAron Hillegass book as well. Do you have a source about it ?

Anyway, I redid the UI with embedding my view inside an empty NSTabView, and the behavior I described still applies so it isn't that...

phjo
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Hmm, I don't seem to be able to find any obvious references. I have always used NSTabView's though, and it wasn't for no reason. <shrug> it may not be the best way to do it.

EDIT: I think this is where I got my information.
 

phjo

macrumors regular
Original poster
Jan 8, 2008
149
1
If I am not misstaken, you would use a tabless NSTabView when you want to be able (not by explicit user interaction) to switch views inside your window...

It makes no sense if you only have one view instance, which is my case... And obviously it does not fix the problem I wrote about.

phjo
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
^^ Ah OK, that is true for me sometimes, and others I'm already using a tab view :eek:.

Sorry for derailing your thread.

Back to the question.

But, it appears that although the text is still there, precisely where it should be, you have too look hard to see it, as it appears in a microscopic size... The lines, rect or whatever objects you drew on the view do appear in the same size as previously...

That is the weirdest thing I've ever seen, it sounds like a bug if I'm honest.

I do agree that printing in Cocoa is pretty horrible, and doesn't work very well, though it doesn't involve writing much code, maybe its worth writing the print code manually and see what happens?
 

phjo

macrumors regular
Original poster
Jan 8, 2008
149
1
I'm still interested in an explanation if you have one, but for the record, I filed a bug report, and I replaced the Quartz text drawing with the AppKit directives of NSString which work fine, for screen and print...

phjo
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.