Using Font Macros for Consistency

Wed, Jul 6

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
[textFieldUserName setFont:DEFAULT_FONT(DEFAULT_FONTSIZE)];
 
// For bold and larger text
[someTextField setFont:DEFAULT_BOLDFONT(DEFAULT_FONTSIZE + 2)];
 
...
 
// 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.

10 comments

nice idea!

by Fil on Jul 6, 2011. #

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

by Brandon on Jul 6, 2011. #

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

like [UIFont fontForRootViewController];

by Bruno on Jul 6, 2011. #

Also, a great idea!

by Trevor on Jul 6, 2011. #

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

by Trevor on Jul 6, 2011. #

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

by Mack on Jul 6, 2011. #

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__);
#else
# define DLog(…)
#endif

by Mark on Jul 6, 2011. #

much like the prefix.pch setup from Marcus Zarra
http://www.cimgf.com/2010/05/02/my-current-prefix-pch-file/

by Bruno on Jul 6, 2011. #

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

by Nik on Jul 6, 2011. #

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

by John Muchow on Jul 6, 2011. #