IOS UIButton Setting both the title and the image properties

Source: Internet
Author: User

In iOS development, use UIButton to set the title and image, to achieve the Tabbaritem effect, that is, the title in the next, image on:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/2F/49/wKioL1OexrfCP8zzAAAI-UZ7fbY747.jpg "title=" screen shot 2014-06-16 pm 6.23.22.png "alt=" Wkiol1oexrfcp8zzaaai-uz7fby747.jpg "/>

At the moment, I find that there are two better ways to do this:

Method one, using uiedgeinsets

uibutton *button = [ uibutton buttonwithtype: Uibuttontypesystem

    [button setframe : cgrectmake (100 , 100 , 60 , 60 )];

[Button setbackgroundcolor: [uicolor greencolor];

UIImage *image = [UIImage imagenamed:@ "index"];

[Button settitle:@ "Home" forstate:uicontrolstatenormal];

[Button setimage: Image forstate:uicontrolstatenormal];

//If not set, the default image is left, title is right, center is displayed

The title and image overlap are centered if the following settings are set

button. titleedgeinsets = uiedgeinsetsmake(0.0,-image. Size. width, 0.0, 0.0);

= uiedgeinsetsmake ( 0.0

button. Titlelabel. Bounds. Size. width);


//If set below, the title moves down 40 units, under, image moves up 20 units, on, center display

     button. titleedgeinsets = uiedgeinsetsmake (40.0 ,-image.size . width , , 0.0 );

= uiedgeinsetsmake (- 20.0

                 button. Titlelabel Bounds Size Width

[self. View addsubview: button];


Method Two, overriding UIButton's Layoutsubviews

-(void) layoutsubviews

{

[super layoutsubviews];

Center = . Imageview Center

    Center. x = . frame . size . width / 2 ;

= Self Imageview Frame Size Height/ 2

Self. ImageView. Center = Center;


CGRect frame = [ self titlelabel]. frame;

    Frame. origin . x = ;

Y = Self Imageview Frame Size Height + 2

frame. Size. Width = self. Frame. Size. width;

Self. Titlelabel. frame = frame;

self Titlelabel Textalignment = Nstextalignmentcenter

[self. Titlelabel setFont: [uifont systemfontofsize:10.0f]];

}


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.