黑马程序员—IOS按钮用法与属性实例

----------------------java培训Android培训IOS培训.Net培训期待与您交流!----------------------

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: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培训期待与您交流!----------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值