Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretchable image stretching part wider than 1px

I would like to create a UIButton which uses a stretchable image as Background image such as that I can easily resize the button for different labels etc.

So I created the following code which works fine:

UIImage *bgImage = [[UIImage imageNamed:@"Button-Template.png"]stretchableImageWithLeftCapWidth:2 topCapHeight:2];
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
loginButton.frame = CGRectMake(180, 280, 112, 40);
[loginButton setBackgroundImage:bgImage forState:UIControlStateNormal];
// some more stuff

Now here is the catch: The iPhone SDK assumes that the strechable part of the image is exactly one pixel in width, which is not the case in my image as I want to create a pattern, which requires a repetition every two pixels (so 2 Pixels are one pattern unit). I have found no information in the docs whether it is possible to alter the value of the strechable part width (in my case 2 instead of 1), does anybody know how to do this or how I could achieve my goal with a workaround? Writing the stretching part on my own seems a little far fetched right now (though I might have to get back to this).

Thanks!

like image 682
Robin Avatar asked Dec 16 '25 22:12

Robin


1 Answers

From iOS 5.0 on you could use resizableImageWithCapInsets:

Check UIImage Class Reference

But this method only works on iOS >= 5.0 devices, so that can be a turn down for now.

like image 183
Rok Jarc Avatar answered Dec 19 '25 16:12

Rok Jarc