Simple Custom button

Source: Internet
Author: User

Inherited from the UIButton

First we need to know why we need to customize the button, because the existing button does not meet our requirements to implement the function

For example, when we want to add some properties to the button, if we write the classification, we should study the progress, obviously this does not add a sub-class to the happy.

We can also customize when we are not satisfied with the layout of the button's internal title and picture, and someone will use the Titleedgeinsert and Imageedgeinsert properties to adjust, the two properties can only meet the simple requirements, a little bit more complicated.

How to add a sub-class:

The first thing to do is to create a class that inherits from the UIButton class

Add the attributes we need directly in the header file

This allows the call to be displayed elsewhere.

So adjust the position of the picture and the text?

Here are two methods, a method that returns the title frame, a frame that returns an image

-(CGRect) Titlerectforcontentrect: (cgrect) contentrect{    cgfloat titlew =;    CGFloat Titleh =;    CGFloat titlex = 0;    CGFloat titley = one;        Return CGRectMake (Titlex, Titley, Self.titlew, Titleh);} -(CGRect) Imagerectforcontentrect: (cgrect) contentrect{    cgfloat ImageX =;     CGFloat imagey = 0;    CGFloat Imagew =;     CGFloat Imageh =;    Return CGRectMake (ImageX, Imagey, Imagew, Imageh);

These two methods, if you want to determine the size of the Textlabel according to the content of the text will be calculated, the calculation method is as follows

Get the width of the caption and the size of the picture cgfloat Titlew = [Self.titleLabel.text boundingrectwithsize:cgsizemake (Maxfloat, maxfloat) options: Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont SystemFontOfSize:18]} Context:nil] . size.width;

In addition to these two methods can also be achieved through the Layoutsubview method

Note that it is important to call the parent class's Layoutsubview method, so that the parent class will first lay out the position of the two controls, we do not need to manage the width of the picture and text, of course, if necessary can also be modified, if you do not need to modify the direct access to their own wide, We can change the position by changing the value of XY so that we can control the position of the picture and the text display.

-(void) layoutsubviews{    //Must call the method of the parent class    [Super Layoutsubviews];    Width height and pitch    cgfloat width = self.bounds.size.width;    CGFloat height = self.bounds.size.height;        CGFloat margin = 5;    The width and height of the picture    cgfloat imagew = self.imageView.image.size.width;    CGFloat Imageh = self.imageView.image.size.height;    Title of frame    cgfloat titlew = self.titleLabel.bounds.size.width;    CGFloat Titleh = height;    CGFloat Titlex = (width-titlew-imagew-margin)/2;    CGFloat Titley = 0;        Self.titleLabel.frame = CGRectMake (Titlex, Titley, Titlew, Titleh);    Image of xy    cgfloat ImageX = titlex + Titlew + margin;    CGFloat imagey = (Height-imageh)/2;        Self.imageView.frame = CGRectMake (ImageX, Imagey, Imagew, Imageh);}

If you want to set some properties of a button while creating it, then we can override the following method

When you create a button using code, you call-(Instancetype) initWithFrame: (cgrect) frame{self    = [Super Initwithframe:frame];    if (self) {                [self setupproperties];            }    return self;} Use storyboard or xib to create a button called-(instancetype) Initwithcoder: (Nscoder *) coder{self    = [Super Initwithcoder:coder] ;    if (self) {                [self setupproperties];            }    return self;} -(void) setupproperties{    //Can be used here to set some properties}

Simple Custom button

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.