28
Apr
2008
 

From Hacker to microISV: Dynamic About Box Version and Copyright, and Text Formatting

by Matt Long

In this post I continue to address topics of relevance to Macintosh programmers who are, like me, moving from being hackers to becoming independent software vendors (micro ISV). Today I am going to address adding copyright and version information to your about dialog and show you how you can update these dynamically. I also cover a few tips on formatting the text in your about dialog. There is more than one way to achieve these things, but my hope is to help others out there who need to accomplish these things and have no prior experience doing them on the Mac.

Note: I have to break here for a moment to give credit where it is due. Marcus Zarra, my cohort on this site has an incredible ability to retain information–especially as it relates to developing applications for the Mac and this has really benefited me greatly. He is a tremendous mentor and teacher and so I felt I needed to just break for a moment and mention that the content of this post is really from his wealth of knowledge. I am writing this post as I am now aware of how to do these things, but it is all thanks to Marcus’ own experience and willingness to share. He’s a true asset to the Mac development community. Ok. Enough of that. Marcus’ head may start to swell. ;-)

Here is the demo application for this post. Download About Box App.

Text Formatting

If you, like me, don’t want to mess with fonts, but you would like to have some control over the formatting of the text in your About Box, then this is the tip for you.

When you first create your application, a default about box is already hooked up for you. You can simply Build and Go and select Application Menu | About Application to see what it looks like. I’m not sure about you, but I’ve never seen a commercial application with the default about box look and style. I doubt you want to leave yours as the default either.

Coming from the Windows world of programming where standards are really just whatever you make them, it may seem unnatural to stick to using the standard fonts, but in the Mac world, if you don’t make your app conform to the standards that are there, you run the risk of making your application the red-headed step child. People will notice and make comments about it. They might even taunt you. :-D

There are some passionate opinions on the topic of the HIG (Human Interfaces Guidlines) and I don’t claim to know much about it, but the bottom line is that your best bet it likely to try to conform to the standards even though many people violate them.

My about box, started out looking something like this:

Default About Box

Most about boxes are pretty boring, but you can’t get too much worse than this one. Instead, I created a new window in Interface Builder and hooked it up to the About menu item. When you create your about box window, make sure your window settings are correct.

About Box Window Attributes

Note: If you don’t ensure that Release When Closed is unchecked, your about box will crash your application. Also, make sure that Visible At Launch is not checked so that your about box doesn’t show up with your application runs.

Then add your various labels etc. so that it looks something like this:

New About Box

Yeah, yeah. I know. Still boring, but we’ll add a couple of features that will help make it more interesting.

Making fonts smaller in a label is easy. Simply select a smaller size in the Size attribute drop down.

Text Field Attributes

You can do this for your version and copyright labels. To make the text larger, however, takes a little more effort. In this case you’ll need to bind the font size attribute to a value in your app delegate.

Add a function to your app delegate that returns a float value representing the size of the font you want:

1
2
3
4
- (float)appNameLabelFontSize;
{
    return 24.0;
}

Then, in interface builder, select your application name label and set the Font size binding to use your AppDelegate‘s appNameLabelFontSize field.

Text Field Bindings

Build and run your application in xcode and you should see the new font size in your about box.

New About Box Bold Name

The final change we want to make to our about box is to make the font bold for our application name. We could create another field in our app delegate to return YES that we could bind the Font bold binding, however, there’s an easier, cooler way that doesn’t require us to do anything but set the Font bold field to something that is already true. In this case we’ll just use the app delegate’s reference to self and then set the Value Transformer to NSIsNotNil.

Text Field Bold Bindings

Since self is always true, this will suffice to tell our label to display in bold.

That’s it for formatting. Now we need to add some features to keep our version and copyright up to date.

Dynamic Version and Copyright

First let’s look at keeping our copyright data up to date as doing so is very similar to the way we set our font size for the app name in the about box. We simply add another field to the app delegate that we can bind to in Interface Builder. Here is the field.

1
2
3
4
- (NSString*)copyrightString;
{
    return @"Copyright © 2008 My Company, Inc.\nAll Rights Reserved.";
}

Just as before, you hook up your field using bindings in Interface Builder. So go back into Interface Builder and select the copyright label placeholder and set the Value binding to copyrightString as shown in the screenshot below.

Copyright Field Bindings

Now, if you want to change your copyright information, you can simply specify it in your copyrightString field in xcode and it will be up to date in your about box.

Finally, you can also use a similar technique to update your version number. This, however, is dependent upon adding a user defined setting to your project build and fiddling with your project Info.plist file. To add a user defined setting to your project build, complete these steps:

  1. Double click your project in the project tree view and click on the Build tab.

  2. At the bottom of the ensuing window, click the drop down and select Add User-Defined Setting as shown. Add User Defined Setting

  3. A new setting will be created in the list. Name it APPLICATION_VERSION and give it a value of 1.0. Close the project settings.
  4. Back in your project tree view, open the Resources sub-directory and select your Info.plist file to edit.
  5. Look for the key value pair for CFBundleVersion and add a new entry as follows:
    1
    2
    3
    4
    
    <key>CFBundleVersion</key>
    <string>Alpha 0.35</string>
    <key>CFBundleShortVersionString</key>
    <string>${APPLICATION_VERSION}</string>

  6. As with our copyright version string, we create another field in the AppDelegate as shown.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    - (NSString*)versionString;
    {
        NSBundle *mainBundle = [NSBundle mainBundle];
        NSDictionary *infoDict = [mainBundle infoDictionary];
     
        NSString *mainString = [infoDict valueForKey:@"CFBundleShortVersionString"];
        NSString *subString = [infoDict valueForKey:@"CFBundleVersion"];
        return [NSString stringWithFormat:@"Version %@ (%@)", mainString, subString];
    }
    The NSDictionary we grab from the main bundle provides the data in our Info.plist file. We simply plug in the values for the keys we’ve found for CFBundleVersion and CFBundleShortVersionString.
  7. Finally, back in Interface Builder, connect the value of the version label to the versionString field of the AppDelegate. Version String Bindings

Now, rebuild and run your project and open your about box. You should now see your version information up to date.

About Box Final

Conclusion

It seems like there is always more to do to get your application finished, but these are the aspects of developing your app that will give it polish. It will look good and will make your life easier. If you start to place build numbers into your version information, it will help you when you get crash reports from your users. This will allow you to know exactly which version of the code caused the problem. And if you are using version control like Git, you can automate this process even further. Take a look at Marcus’s post on Git and XCode: A git build number script for more information. Until next time.

Matt Long

Matt Long works for Colorado Springs iOS Development shop, Skye Road Systems. He is the founder and principal developer there. Matt also works for a startup company called Galen Medical Systems where he develops apps for the medical industry. Contact Matt at Matt at CIMGF dot com to discuss your iOS software development needs. Matt is the co-founder of Cocoa Is My Girlfriend and is the co-author of "Core Animation: Simplified Animation Techniques for Mac and iPhone Development"

More Posts - Website

Follow Me:
Twitter

Comments

xlnxminusx says:

You could also use -[NSApplication orderFrontStandardAboutPanelWithOptions:]

Matt Long says:

@xlnxminusx

Cool! I will take a look at that.

Thanks.

-Matt

Hey Matt,

I posted some info on tracking the build number that you might be interested in.

http://sunflower.coleharbour.ca/cocoamondo/2008/04/tracking-a-build-count-with-xcode/

M@

aclark says:

I can understand the logic behind wanting the copyright string and the version information to be in the code itself. But why not set the font directly in IB?

You could, after all, select the label and bring up the fonts panel to adjust the font size, boldness and other attributes of the label.

Matt Long says:

@aclark

Well I”ll be! You’re right! I make things too hard sometimes. Thanks for the tip.

I can’t believe how obvious that is now. I feel a little sheepish. ;-)

-Matt

Marcus Zarra says:

Matt,

When we ran our tests we did get the font panel to come up … BUT … it was not working at all so we guessed it was a no op. Glad to hear it is working in the latest build of IB.

You can also just use the standard formatting shortcuts with the text field selected, ⌘B to bold the text and ⇧⌘+/⌘- to increase/decrease the font size.

Matt Long says:

@Ciarán Walsh

As Marcus pointed out, we did try this in our tests and it didn’t work. It showed up in IB, but then when we built the app, the fonts were not any different.

Thanks for the tip, though.

-Matt