Common attributes of Lesson 3rd and uibutton

Source: Internet
Author: User

 

1. You can create uibutton using code.

 

1. Common Object Instantiation methods:

Uibutton * button = [[uibutton alloc] initwithframe: rect];

2. Methods for quick Object Instantiation:

Uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect];

 

Tip:

1) In OC development, to instantiate any type of non-custom object, please first try to see if there is a quick definition method.

If a quick definition method exists, try not to instantiate an object using methods like Init!

 

 

 

2. Button Type

1. The content of the uibuttontypecustom button must be customized.

2. uibuttontyperoundedrect rounded rectangle button 3. uibuttontypedetaildisclosure display details button 4. uibuttontypeinfolight color information button, used for dark background 5. uibuttontypeinfodark dark color information button, user light background 6. uibuttontypecontactadd button Description: 1) the first two types of buttons are most commonly used; 2) the last four types of buttons are designed, it is to keep the user's usage habits. 3. Button text in the normal state [Button settitle: @ "button" forstate: uicontrolstatenormal]; long press the button text in the button status [Button settitle: @ "Someone touched me" forstate: uicontrolstatehighlighted]; Note 1) when setting the button text, button status corresponding to text needs to be specified. 4. Button text color

[Button settitlecolor: [uicolor blackcolor] forstate: uicontrolstatenormal];

[Button settitlecolor: [uicolor redcolor] forstate: uicontrolstatehighlighted];

Note 1) when setting the button text color, you also need to specify the button status of the color application. 5. Button background color [Button setbackgroundcolor: [uicolor orangecolor]. Note 1) when setting the background color, the button must be of the custom type. 6. Set the image and background image.

Load image uiimage * image = [uiimage imagenamed: @ "sub_black_add.png"];

Set button image [Button setimage: Image forstate: uicontrolstatenormal];

Set the button background image [Button setbackgroundimage: Image forstate: uicontrolstatenormal];

Note 1) the background image is stretched according to the size of the button. 2) the button image is centered and displayed in the center of the button. 3) If both the image and text of the button are set, • the button area is large enough, images and text are displayed in parallel. • If the area is not large enough, the image is displayed first.
//// Viewcontroller. M // Lesson 3rd, common uibutton attributes /// created by icodephone on 14-7-29. //// # import "viewcontroller. H "@ interface viewcontroller () @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; // define an instance of a button uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect]; // location [Button setframe: cgrectmake (50, 50,200, 40)]; // text (normal or selected) [Button settitle: @ "click the button (normal) "forstate: Ui Controlstatenormal]; [Button settitle: @ "(selected)" forstate: uicontrolstatehighlighted]; // set the text color [Button settitlecolor: [uicolor redcolor] forstate: uicontrolstatenormal]; [Button settitlecolor: [uicolor greencolor] forstate: uicontrolstatehighlighted]; // background color (if you want to set the background color of a button, you need to use a Custom button, that is, uibuttontypecustom) // [Button setbackgroundcolor: [uicolor orangecolor]; // listens to the button (the listening method has only one parameter .) // @ Selector: Tell the system to find this method and load it To the memory. --- (A major feature of OC !) [Button addtarget: Self action: @ selector (onclickbtn :) forcontrolevents: uicontroleventtouchupinside]; // The background image of the set button is stretched according to the size of the button. [Button setbackgroundimage: [uiimage imagenamed: @ "btn001"] forstate: uicontrolstatenormal]; // Add to view [self. view addsubview: button];}/* C #, Java-based, may be written as follows: button. titlelabel. text is enclosed in brackets in OC, indicating the message. In oC, there is no function mechanism. It is a message mechanism. It will tell the system that I need such a message. The system will check whether this method is loaded into the memory. If this method is not in the memory, it loads the method into the memory for execution. * // * 1. When using the link, the returned value of this method is ibaction. The so-called ibaction is the void that can be connected in the header file. 2. When using the link, the method has a parameter. Do not write the parameter */-(void) onclickbtn :( uibutton *) sender {nslog (@ "sender. titlelabel. TEXT = % @ ", sender. titlelabel. text);} @ end

 

--- Icode, love life.

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.