I have created 10 buttons programmatically and set the titles in the button. Now i want to increase the button frame size dynamically,its depends on the text.
I given so
Copy an pasted from Apples Bubble Level source code.
This is the line you want
UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
full code here
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image {
UIButton *button = [[UIButton alloc] initWithFrame:frame];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
[button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenDisabled = YES;
button.adjustsImageWhenHighlighted = YES;
[button setBackgroundColor:[UIColor clearColor]]; // in case the parent view draws with a custom color or gradient, use a transparent color
[button autorelease];
return button;
}