IOS 按钮(button)用法与属性实例
2014-10-06 11:07
351 查看
作者:朱克锋
邮箱:zhukefeng@iboxpay.com
转载请注明出处:http://blog.csdn.net/linux_zkf
IOS 按钮(button)用法与属性实例
- (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:capWidth topCapHeight: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];
}
邮箱:zhukefeng@iboxpay.com
转载请注明出处:http://blog.csdn.net/linux_zkf
IOS 按钮(button)用法与属性实例
- (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:capWidth topCapHeight: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];
}
相关文章推荐
- IOS 按钮(button)用法与属性实例
- IOS 按钮(button)用法与属性实例
- IOS 按钮(button)用法与属性实例
- IOS 按钮(button)用法与属性实例
- 黑马程序员—IOS按钮用法与属性实例
- Android自学笔记之ToggleButton(开关按钮)的功能、特殊属性、用法
- Android自学笔记之Button按钮里的属性及用法
- IOS重写导航栏左侧返回按钮实例
- 属性及反射GetProperties()的一些用法,这里结合网站实例来介绍下
- 页面中设置默认的回车激活按钮, ASP.NET DefaultButton 属性
- IOS 数据保存到属性列表实例
- 微调按钮SpinButton及实例 .
- ruby中类的写法以及属性,实例变量,类方法的用法 .
- ext.ux.ImageButton的用法(图片按钮)
- IOS学习之动态添加Button和监听UIAlertView按钮
- iOS学习之动态添加Button和监听UIAlertView按钮
- android用户界面之按钮(Button)教程实例汇
- 微调按钮SpinButton及实例
- android用户界面之按钮(Button)教程实例汇
- Extjs 利用panel html属性加入DIV实现如TextArea的效果,并且能正确使用html标签,实现如向textarea文本域嵌入按钮button或其他控件,,而且兼容firefox