iOS summary (self-defined button)

Source: Internet
Author: User

1. first create a new class This class inherits UIButton

2. Implementing several methods

1). Change some properties of the font

-(instancetype) initWithFrame: (cgrect) frame{

self = [superinitwithframe: frame];

if (self) {

1--"Sets the position of the font display (left. right)

self. Titlelabel. TextAlignment =nstextalignmentcenter;

2--"Set the size of the font

Self. Titlelabel. font = [uifontsystemfontofsize:];

3--"Set the color of the font

        [ self settitlecolor:[ uicolor colorwithred: 0.33f green: 0.33f blue: 0.33f alpha: 1.00f ] forstate: Uicontrolstatenormal];

        4-- "Color

after selected

        [ self settitlecolor:[ uicolor colorwithred: 0.32f green: 0.74f blue: 1.00f alpha: 1.00f ] forstate: Uicontrolstateselected];

}

return self;

}

In many cases we set the button to have the text (title) and the picture (image) for the position of the text and the picture is only required to complete such as the following two ways to

2). Set the position of the title , the first two properties set the position of the text, and the last two properties set the size of the text

-(cgrect) Titlerectforcontentrect: (cgrect) contentrect{

return CGRectMake (0,contentrect. size. width,N);

}

3). Set the location of the picture, of course, here to set the font in front of the reference and the scope of the view to set, later can be adjusted according to the detailed scope

-(cgrect) Imagerectforcontentrect: (cgrect) contentrect{

return CGRectMake (Contentrect. size. width -up)/2,2, (+ );

}

This method works very well.

iOS summary (self-defined button)

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.