Skip to content

Instantly share code, notes, and snippets.

@nevyn
Created February 6, 2012 15:39
Show Gist options
  • Save nevyn/1752712 to your computer and use it in GitHub Desktop.
Save nevyn/1752712 to your computer and use it in GitHub Desktop.
Make UIButtons from images with a single line
@interface SPAppearance : NSObject
+(id)sharedAppearance;
/** return a button with [name].png for normal, [name]-highlighted.png for highlight state,
[name]-selected.png for selected and [name]-disabled.png for disabled, and combinations thereof.
If the image conforms to configuredImageNamed, its 9-part configuration will be used for the button,
and its title insets configured appropriately.
*/
-(UIButton*)buttonWithImageNamed:(NSString*)name;
/** If there is a [name].9edge.plist or [name.plist] file in the bundle, it will be used to make a stretched
or tiled image from the image with the given name. Otherwise [UIImage imageNamed:] is used unmodified
*/
-(UIImage*)configuredImageNamed:(NSString*)name;
-(UIImage*)configuredImageNamed:(NSString*)name configName:(NSString*)configName; // if different from image name
@end
#import "SPAppearance.h"
@implementation SPAppearance
+(id)sharedAppearance;
{
static SPAppearance *g_singleton;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
g_singleton = [SPAppearance new];
});
return g_singleton;
}
-(UIButton*)buttonWithImageNamed:(NSString*)name;
{
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *normal = [self configuredImageNamed:$sprintf(@"%@", name) configName:name];
UIImage *highlighted = [self configuredImageNamed:$sprintf(@"%@-highlighted", name) configName:name];
UIImage *selected = [self configuredImageNamed:$sprintf(@"%@-selected", name) configName:name];
UIImage *highlightedSelected = [self configuredImageNamed:$sprintf(@"%@-highlighted+selected", name) configName:name];
UIImage *disabled = [self configuredImageNamed:$sprintf(@"%@-disabled", name) configName:name];
if(normal)
[b setBackgroundImage:normal forState:UIControlStateNormal];
if(highlighted)
[b setBackgroundImage:highlighted forState:UIControlStateHighlighted];
if(selected)
[b setBackgroundImage:selected forState:UIControlStateSelected];
if(highlightedSelected)
[b setBackgroundImage:highlightedSelected forState:UIControlStateHighlighted|UIControlStateSelected];
if(disabled)
[b setBackgroundImage:disabled forState:UIControlStateDisabled];
b.titleEdgeInsets = normal.capInsets;
return b;
}
-(UIImage*)configuredImageNamed:(NSString*)name configName:(NSString*)configName;
{
UIImage *img = [UIImage imageNamed:name];
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:configName ofType:@"9edge.plist"]];
if(!settings)
settings = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:configName ofType:@"plist"]];
if(!img || !settings)
return img;
NSNumber *left = [settings objectForKey:@"left"];
NSNumber *top = [settings objectForKey:@"top"];
NSNumber *bottom = [settings objectForKey:@"bottom"];
NSNumber *right = [settings objectForKey:@"right"];
if([[settings objectForKey:@"type"] isEqual:@"strech"])
return [img stretchableImageWithLeftCapWidth:left.floatValue topCapHeight:top.floatValue];
else if([[settings objectForKey:@"type"] isEqual:@"tile"])
return [img resizableImageWithCapInsets:UIEdgeInsetsMake(top.floatValue, left.floatValue, bottom.floatValue, right.floatValue)];
else
NSLog(@"Unknown image configuration type %@", [settings objectForKey:@"type"]);
return img;
}
-(UIImage*)configuredImageNamed:(NSString *)name;
{
return [self configuredImageNamed:name configName:name];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment