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