Use Uiedgeinsetsmake on UIButton to align title with picture

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.