views:

66

answers:

1

I would like to place buttons in various places (not in the nav bar), which have dynamic text. I want them to look similar to black nav bar button items (with the gray and black gradient).

How would I go about creating these? They need to be dynamic width based on the text of the button. I know I could create png files and stretch them, but is there a better way?

A: 

You'll need to create the button yourself using images. Simply create a custom UIButton and assign appropriate images for the various button states you're interested in.

You can use UIImage's 'stretchableImageWithLeftCapWidth' method to create a stretchable image from an image designed to stretch and use NSString's 'sizeWithFont' method to determine what size the button should be.

http://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/stretchableImageWithLeftCapWidth:topCapHeight:

Something like this might do the trick:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

int capHeight = 31; int capWidth = 9; UIImage *buttonTemplate = [UIImage imageNamed:@"button_template.png"]; UIImage *stretchedButtonImage = [buttonTemplate stretchableImageWithLeftCapWidth:capWidth topCapHeight:capHeight]; [button setBackgroundImage:stretchedButtonImage forState:UIControlStateNormal];

Jake