iOS learning-uibutton ImageView and Titlelabel

Source: Internet
Author: User

The location settings of the UIButton ImageView and Titlelabel are set by Setimageedgeinsets and Settitleedgeinsets

Reference: http://blog.csdn.net/dfqin/article/details/37813591 and http://blog.sina.com.cn/s/blog_5df876f301016h8h.html

There are many ways to achieve the effect, like nesting label,imageview on a button, which is achieved by adjusting the button's own Titlelabel and ImageView.

Customize a method to create a button, pass in the text and picture name of Titlelabel, the picture should not be too large, width and height exceeds the width of the button when the picture is compressed

-(UIButton *) Creatbtnwithtitle: (NSString *) title Andimagename: (NSString *) image{UIImage*buttonimage =[UIImage Imagenamed:image]; CGFloat buttonimageviewwidth=cgimagegetwidth (buttonimage.cgimage); CGFloat buttonimageviewheight=cgimagegetheight (buttonimage.cgimage); NSString*buttontitle =title; Uifont*buttontitlefont = [Uifont systemfontofsize:18.0f]; Cgsize buttontitlelabelsize=[Buttontitle Sizewithattributes:@{nsfontattributename:buttontitlefont}]; //button width, at least the sum of imageview width and Titlelabel widthcgfloat buttonwidth= Buttonimageviewwidth +Buttontitlelabelsize.width; //button height, at least the sum of imageview height and Titlelabel heightcgfloat buttonheight= Buttonimageviewheight +Buttontitlelabelsize.height; UIButton*TEMPBTN =[UIButton buttonwithtype: (Uibuttontypecustom)]; [Tempbtn Setbounds:cgrectmake (0,0, Buttonwidth, Buttonheight)];            [Tempbtn.titlelabel Setfont:buttontitlefont];        [Tempbtn setimage:buttonimage Forstate:uicontrolstatenormal];        [Tempbtn.imageview Setbackgroundcolor:[uicolor Clearcolor];        [Tempbtn Settitle:buttontitle Forstate:uicontrolstatenormal];            [Tempbtn.titlelabel Setbackgroundcolor:[uicolor Clearcolor]; Cgpoint Buttonboundscenter=Cgpointmake (Cgrectgetmidx (tempbtn.bounds), Cgrectgetmidy (tempbtn.bounds)); //Find out ImageView's final centerCgpoint Endimageviewcenter= Cgpointmake (buttonboundscenter.x + tempbtn.bounds.size.width/2-tempbtn.imageview.bounds.size.width/2, BUTTONBOUNDSCENTER.Y); //Find out Titlelabel's final centerCgpoint Endtitlelabelcenter= Cgpointmake (buttonboundscenter.x-tempbtn.bounds.size.width/2+ tempbtn.titlelabel.bounds.size.width/2, BUTTONBOUNDSCENTER.Y); //get ImageView's initial centerCgpoint Startimageviewcenter=TempBtn.imageView.center; //get Titlelabel's initial centerCgpoint Starttitlelabelcenter=TempBtn.titleLabel.center; //Set Imageedgeinsetscgfloat imageedgeinsetstop= Endimageviewcenter.y-Startimageviewcenter.y; CGFloat Imageedgeinsetsleft= Endimageviewcenter.x-startimageviewcenter.x; CGFloat Imageedgeinsetsbottom= -Imageedgeinsetstop; CGFloat imageedgeinsetsright= -Imageedgeinsetsleft; Tempbtn.imageedgeinsets=Uiedgeinsetsmake (Imageedgeinsetstop, Imageedgeinsetsleft, Imageedgeinsetsbottom, imageedgeinsetsright); //Set Titleedgeinsetscgfloat titleedgeinsetstop= endtitlelabelcenter.y-Starttitlelabelcenter.y; CGFloat Titleedgeinsetsleft= Endtitlelabelcenter.x-starttitlelabelcenter.x; CGFloat Titleedgeinsetsbottom= -Titleedgeinsetstop; CGFloat titleedgeinsetsright= -Titleedgeinsetsleft; Tempbtn.titleedgeinsets=Uiedgeinsetsmake (Titleedgeinsetstop, Titleedgeinsetsleft, Titleedgeinsetsbottom, titleedgeinsetsright); returntempbtn;}

You can create a button in the implementation method

UIButton *btn = [Self creatbtnwithtitle:@"Dear Princess, please call me"Andimagename:@"Chat_bottom_up_nor"]; Btn.layer.cornerRadius=5;    [btn Settitlecolor:[uicolor Blackcolor] forstate: (UIControlStateNormal)];    [btn Settitlecolor:[uicolor Grouptableviewbackgroundcolor] forstate: (uicontrolstatehighlighted)]; Btn.layer.borderWidth=0.5; Btn.frame= CGRectMake ( -, $, -, -); [Self.view addsubview:btn];

iOS learning-uibutton ImageView and Titlelabel

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.