Use Uiedgeinsetsmake on UIButton to align title with picture
By default, the effect is not set to center the reality, button is 150*150
After using the following settings:
[Self Settitleedgeinsets:uiedgeinsetsmake (0.0,-backgroundimag.size.width, 0.0,0.0)];
[Self setimageedgeinsets:uiedgeinsetsmake (0.0, 0.0,0.0,-self.titlelabel.bounds.size.width)];
To place the title above the picture, move up-80 relative to the picture
[Self settitleedgeinsets:uiedgeinsetsmake ( -80.0,-backgroundimag.size.width, 0.0,0.0)];
[Self setimageedgeinsets:uiedgeinsetsmake (0.0, 0.0,0.0,-self.titlelabel.bounds.size.width)];
The effect is as follows:
In summary, if you set a title or image in the button position, Uiedgeinsets is relative to the button frame to calculate (top, left, bottom, right,), if it is described in the case, The title is set relative to the frame of the image, and the position of the image is set relative to the position of the titel.
over!
Reproduced from: http://blog.csdn.net/yanxiaoqing/article/details/7230660
Example Source:
UIImage * image = [[UIImage imagenamed:@"Telicon"] retain]; NSLog (@"%@>>", Nsstringfromcgsize (image.size)); UIButton * button =[UIButton Buttonwithtype:uibuttontypecustom]; [Button Setbackgroundcolor:[uicolor Yellowcolor]; [Button Setframe:cgrectmake (80,80,150,150)]; [Button Settitle:@"Test location"Forstate:uicontrolstatenormal]; [Button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Button.titlelabel Setfont:[uifont Boldsystemfontofsize:10.0]]; [Button Settitleedgeinsets:uiedgeinsetsmake (0.0,-image.size.width, 0.0, 0.0)]; [Button Setimageedgeinsets:uiedgeinsetsmake (15.0, 0.0, 0.0,-button.titleLabel.bounds.size.width)] ; NSLog (@ "%@", Nsstringfromuiedgeinsets (button.titleedgeinsets)); [Button Setimage:image Forstate:uicontrolstatenormal]; [Self.view Addsubview:button];
Use Uiedgeinsetsmake on UIButton to align title with picture