Learn Objective-C on the Mac - Mark Dalrymple [136]
Figure 13-11. The LOLmaker window, adjusted for scrolling
Now let’s configure the autosizing settings. Doing this will let us not only scroll the LOLView, but resize it along with the window as well. First click the NSScrollView (not the LOLView itself), open the Size Inspector, and configure it to be expandable in both directions, and “tied” to all four window edges. In other words, all the red control lines should be lit solid, none of them grayed out. Then, configure the image well to be stuck to the lower left-hand corner, and the text view to be stuck to the bottom, left, and right edges, and horizontally expandable.
Save all your work, Build & Run, and you are done with the scrollview! Drag in a large image, and you’ll see that it starts off in the lower left, but scrollbars are present, and you can drag around to wherever you want! See this in action in Figure 13-12.
Figure 13-12. Scrollbars: So simple, even a papier-mâché bird can do it.
Drawing Text
Okay, so let’s get to the final step, drawing the text. It is traditional in the LOLcats community to use a text caption in the Impact font, using white text with a black shadow. This should be a piece of cake. The only mildly tricky part is choosing a font size. Because images can be all sorts of sizes, we need to dynamically choose the font size so that the caption fills up a decent portion of the view without extending off the side. We’ll do this by testing several font sizes, starting with 1, and working our way upwards by doubling the size each time, until we hit a point where the size of the text would be wider than the view itself. That we ratchet the font size back down a bit, and draw the text. This is all done by adding the bold lines shown below:
Now, Build & Run, drag in an image, and write some text. Voila! You should see something like Figure 13-13.
Figure 13-13. This is not a cat.
Printing Basics
Most applications that let you do anything interesting on the screen will also let you print your work. On Mac OS X, this is incredibly easy for a developer to implement, thanks in large part to the way that the OS’s display system is designed. The Core Graphics methods and functions that we use to draw into a view can actually be used to draw into a variety of contexts, which means that the system can use our existing drawRect: methods to make us draw our content into a context that’s ready for printing.
The most basic way to print your content is through the use of the print: method, which is implemented both by NSWindow and NSView. You could, for instance, connect a menu item to the print: method on the First Responder object in Interface Builder. That way, whatever object the user selects will be printed if the menu item is selected. That actually points out the downside of that approach, though: because the method is called on the first responder, invoking the print command would cause the text field to be printed if the user is currently editing in a text field!
Normally, you’re probably better off routing your printing through a controller object instead. Presumably, either the window controller, or the document in a document-based application, knows what’s the most appropriate thing to print at any point, typically either the entire window, or a single view that is the focus of the window. Using our latest example, the LOLview is what should really be printed, and we’ll add a method to MyDocument to make this work. Start off by adding the following to the MyDocument class:
Now, open up MainMenu.xib in Interface Builder. We want to connect a menu item to our new printContent: method using the First Responder proxy object, but first we have to configure it so that Interface Builder knows that such a method exists! Start by selecting the