Skip to content

Instantly share code, notes, and snippets.

@puppybits
Created September 17, 2011 15:22
Show Gist options
  • Save puppybits/1224038 to your computer and use it in GitHub Desktop.
Save puppybits/1224038 to your computer and use it in GitHub Desktop.
iOS custom style categories for fonts and colors
// ARC supported. iOS 4.0+
//
// ApplicationStyles.h
//
// Created by Bobby Schultz on 8/11/11.
// Updated 8/23/12 - include fonts, new syntax.
// Copyright 2011 PB. All rights reserved.
// Based on @drawnonward idea from StackOverflow
// http://stackoverflow.com/questions/2824187/objective-c-defining-uicolor-constants
//
// Set colors and font style in Objective-C to be uses application-wide
//
// example:
// //define in appDelegate or konstant file
// SET_COLOR_HEX(transparentGreen, 0x3300FF00);
// SET_COLOR_HEX(specialBlue, 0xFF0000FF);
// SET_COLOR_HEX(badass, 0xFFBAD455);
// SET_FONT(home_tabNormal, @"MyFont", 20);
// SET_FONT_COLOR(home_tabSelected, @"MyFont", 20, 0xFFFF00000)
//
// //anywhere in code access the color through the UIColor class
// [UIColor home_tabNormalColor];
// [UIFont home_tabNormalFont];
// xCode 4.2+ has code hinting as well
#define SET_COLOR_HEX(colorname, hexcolor) \
\
@interface UIColor (Style##colorname) \
\
+(UIColor *) colorname##Color; \
\
@end \
\
@implementation UIColor(Style##colorname) \
\
+ (UIColor*) colorname##Color \
{ \
static UIColor * staticColor = nil; \
static dispatch_once_t token; \
dispatch_once(&token, ^{ \
float red = ((hexcolor & 0xFF0000) >> 16)/255.0; \
float green = ((hexcolor & 0xFF00) >> 8)/255.0; \
float blue = (hexcolor & 0xFF)/255.0; \
float alpha = (hexcolor >> 24)/255.0; \
\
staticColor = [[UIColor alloc] initWithRed:red \
green:green \
blue:blue \
alpha:alpha]; \
}); \
return staticColor; \
} \
@end
#define SET_FONT(name, fontfamily, fontsize) \
\
@interface UIFont (Style##name) \
\
+(UIFont *) name##Font; \
\
@end \
\
@implementation UIFont(Style##name) \
\
+ (UIFont*) name##Font \
{ \
static UIFont * staticFont = nil; \
static dispatch_once_t token; \
dispatch_once(&token, ^{ \
staticFont = [UIFont fontWithName:fontfamily size:fontsize]; \
}); \
return staticFont; \
} \
@end \
#define SET_FONT_COLOR(name, fontfamily, fontsize, hexcolor) \
\
@interface UIFont (Style##name) \
\
+(UIFont *) name##Font; \
\
@end \
\
@implementation UIFont(Style##name) \
\
+ (UIFont*) name##Font \
{ \
static UIFont * staticFont = nil; \
static dispatch_once_t token; \
dispatch_once(&token, ^{ \
staticFont = [UIFont fontWithName:fontfamily size:fontsize]; \
}); \
return staticFont; \
} \
@end \
\
@interface UIColor (Style##name) \
\
+(UIColor *) name##Color; \
\
@end \
\
@implementation UIColor(Style##name) \
\
+ (UIColor*) name##Color \
{ \
static UIColor * staticColor = nil; \
static dispatch_once_t token; \
dispatch_once(&token, ^{ \
float red = ((hexcolor & 0xFF0000) >> 16)/255.0; \
float green = ((hexcolor & 0xFF00) >> 8)/255.0; \
float blue = (hexcolor & 0xFF)/255.0; \
float alpha = (hexcolor >> 24)/255.0; \
\
staticColor = [[UIColor alloc] initWithRed:red \
green:green \
blue:blue \
alpha:alpha]; \
}); \
return staticColor; \
} \
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment