oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Build Your First Cocoa App
Pages: 1, 2, 3, 4

While we're here, let's modify a few things about the way our text view will act. Select the Text view and open the Inspector by choosing View Info in the Tools menu. The Info window should be labeled "NSTextView info"; if not, click on your text view again. An attributes info view should be displayed in the Inspector. If this is not shown, select it from the pull-down menu at the top of the window.

Screenshot of the Attributes Info view.
The Attributes info view.

Now we want to enable the options for importing graphics and implementing undo services. The options are labeled "Graphics allowed" and "Undo allowed"; click the checkboxes so they are enabled.

Finally, select Size from the pop-up menu at the top of the Info window. This will allow us to set how the document window and text view will resize relative to each other when the entire window is resized. Click on the horizontal and vertical lines in the box within the window so that they look like springs. What this does is tell our application that when we resize the document window, the distance between the window border and the text view edges must always remain constant; the springs indicate that the size of the text view is free to resize so that this restriction can be accommodated.

Screenshot of specifying resize options.
Setting the resize options.

The other option would be to change the lines connecting the text view edge to the window edge to springs (with the lines interior to the text view solid), which would cause the text view size to remain fixed when the window is resized, and the space between the two to freely change.

OK, so save the NIB file, and return to Project Builder where we will build and run our application, and with no coding, see all the free features provided by Cocoa.

Building and running the application

Building and running an application are achieved by clicking on the hammer and monitor icons in the toolbar (you must wait for the application to finish compiling before you run it). Alternatively you can press command-R to accomplish both of these things.

When your application starts running, you will see a blank document window appear with the name of your document in the title, "untitled". Play around with the menus and take note of what commands are available. Spellcheck. Kerning. Ligature. Undo. And there are others....

You're probably a bit underwhelmed right now because you think that those menu items can't possibly do anything useful since you didn't write any code; they're just placeholders so you'll remember to implement them later. Think again. Type some text in the window and try them out, see what happens. Have some fun with it. Play with them all!

That's right, they all work! You get text and font styling, and even spellchecking and undo all for nothing! Remember when we enabled the "Graphics allowed" option in Interface Builder? Try dragging an image file -- any image file -- onto your window. It gets pasted in there! Are you impressed yet? This stuff is amazing!

This is the power of the Application Kit and Cocoa; you get all of this just because you had the good sense to own a Mac and try out Cocoa. And we're just scratching the surface of what's possible; this won't be the last time to see something like this. Congratulations! Welcome to the world of Cocoa!

What just happened?

So how is it that we get all this stuff for free? Let me explain. When we dragged that text window onto the main window of our application, we were creating an instance of the NSTextView class in out application. Once we did that, we incorporated a wealth of code and functionality contained in NSTextView and its parent class, NSText. That is where the magic happens.

NSText is a great example of what I talked about in the first column -- Apple has put a lot of work into providing you with a rich set of tools to start out with so you don't have to reinvent the wheel. What NSText provides is code that implements all the cool things we saw: kerning, font styling, spell checking, alignment, and rulers. All of this is programmed into the NSText class, and we interact with it through instances of its subclass, NSTextView. I encourage you to take a look through the class documentation for NSText and NSTextView to gain a more detailed understanding of what's going on.

What we don't get for free

Unfortunately we can't get everything for free, but we're not doing so bad this far. You don't get to save your document yet, and open saved documents. However, it's not hard at all to add this, and the next column will focus on this.

Next time, we will take this application and add some code to it giving it some more functionality such as saving and opening files. We'll also learn more about document-based applications -- another wonderful freebie in Cocoa-as well as how to work with data.

See you in two weeks!

Michael Beam is a software engineer in the energy industry specializing in seismic application development on Linux with C++ and Qt. He lives in Houston, Texas with his wife and son.

Read more Programming With Cocoa columns.

Return to the Mac DevCenter.