I want to show image and title both in a button, then i try the setImageEdgeInsets and setTitleEdgeInsets.But it makes me confused.

Before setting the insets property, the button likes below ( Black rect represent the button bounds, Red rect represent the image rect, Green rect represent the title rect.)

Additional Description: My image's actual size is twice of i useļ¼Œ which means

**  [image size].width = imageWidth * 2  **

enter image description here

then i use setImageEdgeInsets function, to set image rect

    [btn setImageEdgeInsets:UIEdgeInsetsMake((btnHeight-titleHeight-imageHeight)/2.0,
                                              (btnWidth-imageWidth)/2,
                                              titleHeight+(btnHeight-titleHeight-imageHeight)/2.0,
                                              (btnWidth-imageWidth)/2)];

Now the button likes this

enter image description here

Then i use setTitleEdgeInsets function, to set title rect

I first think code should be:

[btn setTitleEdgeInsets:UIEdgeInsetsMake(btnHeight-titleHeight, 0, 0, 0)];

but the button in fact likes this

enter image description here

i also try code:

[btn setTitleEdgeInsets:UIEdgeInsetsMake(btnHeight-titleHeight, -imageWidth, 0, 0)];

Button now likes this

enter image description here

Then i try code:

[btn setTitleEdgeInsets:UIEdgeInsetsMake(btnHeight-titleHeight, -imageWidth*2, 0, 0)];

It's effective!

enter image description here

But i'm confused now! why the title's left inset should be negative? and why does it related to image's width?

0

There are 0 answers