// // UIButton+NavButton.m // // Created by Michael Heyeck on 11/9/09. // Copyright 2009 Fair Oaks Labs, Inc. // #import "UIButton+NavButton.h" @implementation UIButton (NavButton) - (UIButton*)configureForBackButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action { // Experimentally determined CGFloat padTRL[3] = {6, 8, 12}; // Text must be put in its own UIView, s.t. it can be positioned to mimic system buttons UILabel* label = [[UILabel alloc] init]; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont boldSystemFontOfSize:12]; label.textColor = [UIColor whiteColor]; label.shadowColor = [UIColor darkGrayColor]; label.shadowOffset = CGSizeMake(0, -1); label.text = title; [label sizeToFit]; // The underlying art files must be added to the project UIImage* norm = [[UIImage imageNamed:@"back_norm.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0]; UIImage* click = [[UIImage imageNamed:@"back_click.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0]; [self setBackgroundImage:norm forState:UIControlStateNormal]; [self setBackgroundImage:click forState:UIControlStateHighlighted]; [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; // Calculate dimensions CGSize labelSize = label.frame.size; CGFloat controlWidth = labelSize.width+padTRL[1]+padTRL[2]; controlWidth = controlWidth>=norm.size.width?controlWidth:norm.size.width; // Assemble and size the views self.frame = CGRectMake(0, 0, controlWidth, 30); [self addSubview:label]; label.frame = CGRectMake(padTRL[2], padTRL[0], labelSize.width, labelSize.height); // Clean up [label release]; return self; } @end