IOS button usage vs. attribute instance

Source: Internet
Author: User

IOS button usage vs. attribute instance

-(void) ToggleButton: (UIButton *) button

{

if (isOn =! ) IsOn)

{

[Button Settitle:@ "on" forstate: UIControlStateNormal];

[Button Settitle:@ "on" forstate: uicontrolstatehighlighted];

[Button setbackgroundimage:basegreen forstate: UIControlStateNormal];

[Button setbackgroundimage:altgreen forstate: uicontrolstatehighlighted];

}

Else

{

[Button Settitle:@ "Off" forstate: UIControlStateNormal];

[Button Settitle:@ "Off" forstate: uicontrolstatehighlighted];

[Button setbackgroundimage:basered forstate: UIControlStateNormal];

[Button setbackgroundimage:altred forstate: uicontrolstatehighlighted];

}

}

-(void) Viewdidload

{

float capwidth = 110.0f;

Basegreen = [[[UIImage imagenamed:@ ' green.png '] stretchableimagewithleftcapwidth: Capwidth topcapheight:0.0f] retain];

basered = [[[UIImage imagenamed:@ "Red.png"] stretchableimagewithleftcapwidth: capwidth Topcapheight:0.0f] retain];

Altgreen = [[[UIImage imagenamed:@ "Green2.png"] Stretchableimagewithleftcapwidth : Capwidth topcapheight:0.0f] retain];

altred = [[[UIImage imagenamed:@ "Red2.png"] stretchableimagewithleftcapwidth: capwidth Topcapheight:0.0f]retain];

Create

UIButton *button = [UIButton buttonwithtype: Uibuttontypecustom];

button. frame = CGRectMake (0.0f, 0.0f, 300.0f, 233.0f);

button. Center = Cgpointmake (160.0f, 140.0f);

Setting the Aligment property

button. contentverticalalignment = Uicontrolcontentverticalalignmentcenter;

button. contenthorizontalalignment = Uicontrolcontenthorizontalalignmentcenter;

Button.titlelabel. TextAlignment = uitextalignmentcenter;

set the title adaptive Alignment

Button.titlelabel. Linebreakmode = uilinebreakmodewordwrap;

setting colors and Fonts

[Button Settitlecolor: [uicolor whitecolor] forstate: UIControlStateNormal];

[Button Settitlecolor: [uicolor lightgraycolor] forstate: uicontrolstatehighlighted];

Button.titlelabel. Font = [Uifont boldsystemfontofsize:24.0f];

Add Action

[Button AddTarget:self Action:@selector(toggleButton:) forcontrolevents: UIControlEventTouchUpInside];

Set Title

[Button Settitle:@ "on" forstate: UIControlStateNormal];

[Button Settitle:@ "on" forstate: uicontrolstatehighlighted];

Set Background

[Button setbackgroundimage:basegreen forstate: UIControlStateNormal];

[Button setbackgroundimage:altgreen forstate: uicontrolstatehighlighted];

BOOL for testing

IsOn = NO;

put the button into the view

[Self. view Addsubview:button];

}

IOS button usage vs. attribute instance

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.