Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [10]

By Root 929 0
this book, but we will walk through the non-obvious ones. As you make your way through the book, you’ll become comfortable with most of the attributes that you’ll find yourself using.

Let’s change the size of the label. If the label is not selected, single-click it to select it. A dot should appear on either side of the label. These dots are resize handles, which allow you to change the size of the selected item. Most objects in Interface Builder have four resize handles, one in each corner, which allow you to adjust the size in all four directions. Certain items, like labels, only have two resize handles. The label’s attributes (the size of its font in particular) determine the vertical size of the label. You don’t change the height of the label by resizing it. You only use the resize handles to change the width of the label.

Let’s center the label. Make sure the left side of the label is lined up with the blue guideline near the left edge of the window. Then, grab the right resize handle and drag out the label until you get to the blue guideline near the right edge of the window. Once you’re done, your label should look like Figure 2-12.

Figure 2-12. Your application’s window in Interface Builder, after resizing the label

Now, with the label still selected, bring up the attributes inspector by typing ⌘1. In the attributes inspector, look for a row of buttons labeled Alignment, and select the “center text” button (Figure 2-13). Also, look for a button labeled Selectable under the heading Behavior. Click that checkbox, which tells Cocoa that we want to allow the user copy this label to the pasteboard if they want. By default, labels are not selectable, but we just changed that.

Figure 2-13. The alignment buttons in the Attributes Inspector for the label, set to centered text

Change the Label’s Color and Font


Let’s make one final change to our window’s content: let’s change the font, size, and color of the text. If you look at the attributes inspector, you can probably figure out how to change the color of the text, but you may be wondering if it’s even possible to change the font and size. Fear not: it is. But you don’t change font and size in the attributes inspector.

First, let’s set the color. Look in the attributes inspector for a color well labeled Text (Figure 2-14). If you click that, the standard Mac OS X color picker will appear (Figure 2- 15), and you can select the color you want for the text. Go ahead and do that now, picking whatever color strikes your fancy.

Figure 2-14. The color well from the Attributes Inspector used for changing the color of the label

Figure 2-15. The standard Mac OS X color picker is used for selecting colors in Cocoa applications. Here, we’re using it in Interface Builder to set the color of our text.

Interface Builder itself is built using Cocoa, and leverages a lot of built-in Cocoa functionality, such as the standard color picker. The Apple engineers do not want to re-invent the wheel any more than you do. When you write your own applications, you’ll be able to use this exact same color picker with just a few lines of code or, in some situations, without writing any code at all.

Another piece of built-in Mac OS X functionality that you’ll be able to use in your applications is the standard font window (Figure 2-16), which allows you to change the font, size, and attributes of selected text. Press ⌘T to bring up the font window in Interface Builder. Make sure that your label is still selected (look for the resize handles), and also make sure that your application’s main window is still the frontmost window.

Play with the font window and make your text look just the way you want it to. Because this application will only be running on your machine, you can select any font and combination of attributes that you wish. When creating applications that will be distributed to others, it is important to realize that you could select fonts that your user won’t have installed. In general, for standard GUI components, you’re probably better not changing

Return Main Page Previous Page Next Page

®Online Book Reader