UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets?

Posted by R31n4ld0_ on Stack Overflow See other posts from Stack Overflow or by R31n4ld0_
Published on 2010-03-15T23:35:00Z Indexed on 2010/03/15 23:39 UTC
Read the original article Hit count: 625

Filed under:
|
|

Hello, guys.

If I put only an image in a button and set the imageEdgeInsets more close to the top, the image stays centered and all works as expected:

[button setImage:image forState:UIControlStateNormal];
[button setImageEdgeInsets:UIEdgeInsetsMake(-15.0, 0.0, 0.0, 0.0)];

If I put only a text in a button and set titleEdgeInsets more close to the bottom, the text stays centered and all works as expected:

[distractionButton setTitle:title forState:UIControlStateNormal];
[distractionButton setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, -30, 0.0)];

But, if i put the 4 lines together one interfere with other and both lost the center alignment.

All my images has 30 pixels widht, and if i put 30 in the left parameter of UIEdgeInsetMake for setTitleEdgeInsets, the text is centered again. The problem is that the image never gets centered because appears that it is dependent of the button.titleLabel size. I already tried many calculations with button size, image size, titleLabel size and never get both perfectly centered.

Someone already have the same problem?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uibutton