黒馬プログラマー-IOSボタンの使い方と属性の例
2635 ワード
-----------------------javaトレーニング、Androidトレーニング、IOSトレーニング、.Netトレーニングはあなたと交流することを期待します!-----------------------
IOSボタンの使い方と属性の例
-----------------------------javaトレーニング、Androidトレーニング、IOSトレーニング、.Netトレーニングはあなたと交流することを期待します!-----------------------
IOSボタンの使い方と属性の例
(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:capWidthtopCapHeight:0.0f] retain];
baseRed = [[[UIImage imageNamed:@"red.png"] stretchableImageWithLeftCapWidth:capWidthtopCapHeight:0.0f] retain];
altGreen = [[[UIImage imageNamed:@"green2.png"] stretchableImageWithLeftCapWidth:capWidthtopCapHeight:0.0f] retain];
altRed = [[[UIImage imageNamed:@"red2.png"] stretchableImageWithLeftCapWidth:capWidthtopCapHeight:0.0f] retain];
//
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, 300.0f, 233.0f);
button.center = CGPointMake(160.0f, 140.0f);
// aligment
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//button.titleLabel.textAlignment = UITextAlignmentCenter;
// title
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
//
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
button.titleLabel.font = [UIFont boldSystemFontOfSize:24.0f];
// action
[button addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
// title
[button setTitle:@"On" forState:UIControlStateNormal];
[button setTitle:@"On" forState:UIControlStateHighlighted];
//
[button setBackgroundImage:baseGreen forState:UIControlStateNormal];
[button setBackgroundImage:altGreen forState:UIControlStateHighlighted];
// BOOL
isOn = NO;
// button view
[self.view addSubview:button];
}
-----------------------------javaトレーニング、Androidトレーニング、IOSトレーニング、.Netトレーニングはあなたと交流することを期待します!-----------------------