iOS Summary _ Achieve UIButton graphics and text mix, while displaying words and pictures

Source: Internet
Author: User

We often need to use buttons that require text and images to be displayed simultaneously, for example:


We need to define a UIButton subclass.


Implementation file

-(ID) initWithFrame: (cgrect) frame

{

self = [super initwithframe: frame];

if (self) {

//Can be adjusted according to your own needs

self. Titlelabel. TextAlignment=nstextalignmentright;

Self. Titlelabel. font=[uifontsystemfontofsize:14.0];

self. ImageView . Contentmode = Uiviewcontentmodeleft;

}

return self;

}

Methods for overriding parent class UIButton


Rect with a more button set and returns the text label of the RECT

-(cgrect) Titlerectforcontentrect: (cgrect) contentrect

{

cgfloat Titlew = contentrect. size. width-+;

cgfloat Titleh = contentrect. size. height;

cgfloat Titlex = 0;

cgfloat Titley = 0;

Contentrect = (cgrect) {{Titlex,titley},{titlew,titleh}};

return Contentrect;

}


Rect with a more button set and returns a Uiimageview rect

-(cgrect) Imagerectforcontentrect: (cgrect) contentrect

{

cgfloat Imagew = ;

cgfloat Imageh = ;

cgfloat ImageX = contentrect. size. width-up;

cgfloat imagey = 2.5;

Contentrect = (cgrect) {{Imagex,imagey},{imagew,imageh}};

return Contentrect;

}


Using subclasses of UIButton

Yxpbutton *selectareabutton=[[yxpbuttonalloc] initwithframe:cgrectmake (5, +) ];

[Selectareabutton settitle:@ "graphic mixed"forstate: UIControlStateNormal];

[Selectareabutton setimage: [UIImageimagenamed:@ "Location" ]forstate:uicontrolstatenormal];

[Selectareabutton settitlecolor: [uicolorwhitecolor] Forstate:uicontrolstatenormal];

[NavigationbgviewAddsubview: Selectareabutton];


So that you can implement a button of the graphic mixed

Related Article

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.