iOS Foundation-uibutton

Source: Internet
Author: User

reasoning, UIButton is a button, is usually we use the finger to click on a control, and then have the corresponding event generated, when clicked may also produce color changes, this is called UIbutton .

It can display text, you can also display pictures, you can dynamically adjust the internal picture and text, but also can add the attribute string, as to what the attributed string is, do not understand the words, I will talk about later.

There are three states of UIButton:

    1. Normal ( normal state )

Default status ( default )

the corresponding enumeration constants: UIControlStateNormal

    1. highlighted ( highlight State )

When the button is pressed (the finger is not released)

the corresponding enumeration constants: uiconrolstatehighlighted

    1. disabled ( )

if enabled property is 0 , is in the disable State, which means the button cannot be clicked.

the corresponding enumeration constants: uicontrolstatedisabled


The following is a description of UIButton's properties and methods in the code.

-(void) viewdidload {    [Super viewdidload];   /*     typedef ns_enum (Nsinteger, UIB Uttontype) {     Uibuttontypecustom = 0,                   &NB Sp    //Custom style      Uibuttontypesystem ns_enum_available_ios (7_0), //Standard system button      Uibuttontypedetaildisclosure,                  //Blue small Arrow Press  button, mainly for detailed instructions      uibuttontypeinfolight,                         //Bright exclamation      Uibuttontypeinfodark,               & nbsp          //Dark exclamation point      Uibuttontypecontactadd,                        //plus button      Uibuttontyperoundedrect = Uibuttontypesystem,  //system default &nbsP Rounded Rectangle button     };     */   //Create a button with a rounded rectangle, and of course the system default     UIButton *btn = [U IButton buttonwithtype:uibuttontyperoundedrect];       //frame contains the coordinates of the control and the width height, where the coordinates are set directly (20,  20), Width high (+)     Btn.frame = CGRectMake (+,, +);       //Set button background color     Btn.backgroundcolor = [Uicolor greencolor];       //Set the render color of the button     Btn.tintcolor = [Uicolor blackcolor];       /*     Set the status here, general Settings Normal and highlight state, Of course there are other statuses to choose from      uicontrolstatenormal       = 0,             & nbsp         //Normal status      uicontrolstatehighlighted  = 1 << 0,                 //Highlight status      uicontrolstatedisabled     = 1 << 1,                  /disable status      uicontrolstateselected     = 1 << 2,                 //Check status      uicontrolstateapplication  = 0x00ff0000,    & nbsp         //When application logo      uicontrolstatereserved     = 0xff000000              //internal reservation      */   //Set button fill picture in normal state     [BTN Setima Ge:[uiimage imagenamed:@ "2.png" forstate:uicontrolstatenormal];   //Settings button's fill picture is highlighted when clicked     [ BTN setimage:[uiimage imagenamed:@ "1.png" forstate:uicontrolstatehighlighted];   //Set the text displayed on the button     [btn settitle:@] "forstate:uicontrolstatenormal];   //Set the color of the text on the button     [btn settitlecolor:[ Uicolor Redcolor] forstate:uicontrolstatenormal];   //Set the shadow color of the text on the button     [btn settitleshadowcolor:[ Uicolor Bluecolor] Forstate:uicontrolstatenormal];   //Settings button background picture     [btn setbackgroundimage:[uiimage imagenamed:@ "] forstate: uicontrolstatenormal];   //Set the property string for the button here if you don't know what a property string can skip     nsattributedstring * attrstring =[[ Nsattributedstring Alloc] initwithstring:@ "Set property string"];    [btn setattributedtitle:attrstring forstate:   UIControlStateNormal];  Set the interior spacing of the internal picture of the button    //top bottom   left right    //uiedgeinsets inserts = Uiedgeinsetsmake (0, 0, 0, 0);   //By default, when the button is highlighted, the color of the image will be darker, this is set to No, then the state is canceled     btn.adjustsimagewhenhighlighted = no;   //By default, when the button is disabled, the color of the graphic will be darker, and if it is set to NO, then the status will be canceled     btn.adjustsimagewhendisabled = NO;    //When set to YES here, the button will glow when pressed     btn.showstouchwhenhighlighted = yes;       /*     uicontroleventtouchdown       //Single Touch press event: The user touches the screen, or a new finger falls  & nbsp   uicontroleventtouchdownrepeat //Multi-touch press event, tapCount greater than 1: when the user presses the second to third or fourth finger.      uicontroleventtouchdraginside //When a touch is dragged within the control window.      Uicontroleventtouchdragoutside//When a touch is dragged outside the control window.      Uicontroleventtouchdragenter  //When a touch is dragged from outside the control window to the inside.      uicontroleventtouchdragexit   //When a touch is dragged from inside the control window to the outside.      uicontroleventtouchupinside   //All touch lift events within the control.      Uicontroleventtouchupoutside  //All touch lift events outside the control (the touch must start with the inside of the control to send a notification).      uicontroleventtouchcancel     //all touch cancellation events, that is, one touch is canceled because it has too many fingers, or is locked or interrupted by a phone call.      uicontroleventtouchchanged    //notification is sent when the value of the control changes. Used for sliders, segmented controls, and other controls that take values. You can configure when a slider control sends notifications when the slider is dropped, or when it is dragged.      uicontroleventeditingdidbegin //Send a notification when you start editing in a text control.      uicontroleventeditingchanged  //Send notification when text in text controls is changed.      uicontroleventeditingdidend   //Send a notification when editing ends in a text control.      UicontroleveNteditingdidonexit//When the edit is finished by pressing ENTER (or equivalent behavior) within the text control, a notification is sent.      uicontroleventalltouchevents  //Notify all touch events.      uicontroleventalleditingevents//Notifies all events about text editing.      uicontroleventallevents       //Notify all events.      */   //Add a Click event to the button     [btn addtarget:self action: @selector (Btnclick) forcontrolevents:uicontroleventtouchupinside];   //Delete all event handling in one State    //[BTN Removetarget:nil Action:nil forcontrolevents:uicontroleventtouchupinside];       //Add a long press event to the button     Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:self action:@ Selector (btnlong:)];   //setting by how long events will trigger long press events     longpress.minimumpressduration = 1;   // Add an event to the button     [btn addgesturerecognizer:longpress];       //Add a button to the controller's view    //Note, don't forget this step     [Self.view AddsubviEW:BTN];} -(void) btnclick{    NSLog (@ "%s", __func__);} -(void) Btnlong: (Uilongpressgesturerecognizer *) gesturerecognizer{    if ([gesturerecognizer state] = = Uigesturerecognizerstatebegan)     {        NSLog (@ "Long press event");        Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "message message:@" OK to delete? "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" delete ", nil];        [Alert show];  & nbsp }}


iOS Foundation-uibutton

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.