iOS Button
iOS按钮(button)用法与属性实例
- (void) toggleButton: (UIButton*) button
{
if(isOn= !isOn)
{
[buttonsetTitle:@"On"forState:UIControlStateNormal];
[buttonsetTitle:@"On"forState:UIControlStateHighlighted];
[buttonsetBackgroundImage:baseGreenforState:UIControlStateNormal];
[buttonsetBackgroundImage:altGreenforState:UIControlStateHighlighted];
}
else
{
[buttonsetTitle:@"Off"forState:UIControlStateNormal];
[buttonsetTitle:@"Off"forState:UIControlStateHighlighted];
[buttonsetBackgroundImage:baseRedforState:UIControlStateNormal];
[buttonsetBackgroundImage:altRedforState:UIControlStateHighlighted];
}
}
- (void) viewDidLoad
{
floatcapWidth =110.0f;
baseGreen= [[[UIImageimageNamed:@"green.png"]stretchableImageWithLeftCapWidth:capWidthtopCapHeight:0.0f]retain];
baseRed= [[[UIImageimageNamed:@"red.png"]stretchableImageWithLeftCapWidth:capWidthtopCapHeight:0.0f]retain];
altGreen= [[[UIImageimageNamed:@"green2.png"]stretchableImageWithLeftCapWidth:capWidthtopCapHeight:0.0f]retain];
altRed= [[[UIImageimageNamed:@"red2.png"]stretchableImageWithLeftCapWidth:capWidthtopCapHeight:0.0f]retain];
//创建
UIButton*button = [UIButtonbuttonWithType: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;
//设置颜色和字体
[buttonsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorlightGrayColor]forState:UIControlStateHighlighted];
button.titleLabel.font= [UIFontboldSystemFontOfSize:24.0f];
//添加action
[buttonaddTarget:selfaction:@selector(toggleButton:)forControlEvents:UIControlEventTouchUpInside];
//设置title
[buttonsetTitle:@"On"forState:UIControlStateNormal];
[buttonsetTitle:@"On"forState:UIControlStateHighlighted];
//设置背景
[buttonsetBackgroundImage:baseGreenforState:UIControlStateNormal];
[buttonsetBackgroundImage:altGreenforState:UIControlStateHighlighted];
//用于测试的BOOL
isOn=NO;
//把button放入view
[self.viewaddSubview:button];
}
参考网址:http://blog.csdn.net/linux_zkf/article/details/7788627
网友评论