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