Using Font Macros for Consistency

In most applications, keeping with one or two fonts is a good design choice. To this end, I often include a few simple macros to keep fonts consistent, easily change font size as well as emphasis (bold).

Below are a few macros that give you an idea how I might go about specifying two primary fonts for an app:

// For text, messages, etc
#define DEFAULT_FONTSIZE    15
#define DEFAULT_FONT(s)     [UIFont fontWithName:@"ArialMT" size:s]
#define DEFAULT_BOLDFONT(s) [UIFont fontWithName:@"Arial-BoldMT" size:s]
// For table cells
#define CELL_FONTSIZE    16
#define CELL_FONT(s)     [UIFont fontWithName:@"Helvetica-Oblique" size:s]
#define CELL_BOLDFONT(s) [UIFont fontWithName:@"Helvetica-BoldOblique" size:s]

With these macros in defined it is easy to specify fonts based on where they are used and keep things consistent across an app:

// Assume textFieldUserName is a UITextField
// For bold and larger text
// Set cell font sizes
// Assume cell is a UITableViewCell
[[cell textLabel] setFont:CELL_FONT(CELL_FONTSIZE)];
// For bold and larger text
[[cell textLabel] setFont:CELL_BOLDFONT(CELL_FONTSIZE + 2)];

You could also add a few additional macros for fonts that support italics or other attributes. If you have similar macros, please feel free to share in the comments section.

  1. Oh my God! You just explained macros to me! I’ve wondered what the #define did… thank you so much!

  2. I am using a uifont category for these kind of properties.

    like [UIFont fontForRootViewController];

  3. I typically create a macro only containing the font name. This is much better! Thanks!

  4. Wow…this is exactly what I was looking for today! Thank you!

  5. Great idea! Just implemented it in one of my projects, though for UILabels and such, i typically just set the font size in Interface Builder.

    Here’s another macro that I use… for a NSLog() replacement while DEBUG is defined so i can turn off all debug messages when i publish my app:

    #ifdef DEBUG
    # define DLog(fmt, …) NSLog((@”%s [Line %d] ” fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
    # define DLog(…)

  6. But why, of all things, would you mix Arial and Helvetica? ;)

    • lol, just take the example for what it is, an example, not necessarily a recommendation on font selection :)

Comments are closed.