美文网首页
iOS开发工程师学习笔记_第三周@geekband

iOS开发工程师学习笔记_第三周@geekband

作者: Andyzzy | 来源:发表于2016-05-25 17:41 被阅读0次

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

相关文章

网友评论

      本文标题:iOS开发工程师学习笔记_第三周@geekband

      本文链接:https://www.haomeiwen.com/subject/kqutdttx.html