UIButton: how to center an image and a text using imageEdgeInsets and titleEdgeInsets?
- by R31n4ld0_
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.