stretch image's both sides but not center for uibutton

868 Views Asked by At

I got an image,which is shorter than button's width, so I want to stretch the image's both sides to fit button's width but not stretch its center because there is text in center. What should I do to achieve that? Appreciate your suggestion!

2

There are 2 best solutions below

1
On

Look for resizableImageWithCapInsets method of UIImage.

[[UIImage imageNamed:@"image"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10) resizingMode:UIImageResizingModeStretch];

When 10 parameter is the margin of the text inside the image.

This creates a new UIImage that you can stretch.

0
On