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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 7,543评论 2 50
  • UIWindow 初始化: self.window= [[UIWindowalloc]initWithFrame:...
    Sunny_Fight阅读 4,668评论 0 1
  • // //ViewController.m //UI常用控件 // //Created bylanou on 16...
    moonlun阅读 1,313评论 0 1
  • 知道吗?有时候,你就是别人,,, 致那些喜欢在背后说别人坏话的人
    彭彭彭彭辉阅读 1,085评论 0 0
  • 作者是我非常推崇的。书也是惦记许多年的。 从高中看过这本书的节选后,就一直想看看。一晃竟然十年了,在假期第一天《我...
    木子一叶阅读 7,530评论 0 2