A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 贺羽昌 中级黑马   /  2014-12-18 14:17  /  602 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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:capWidth topCapHeight:0.0f] retain];

baseRed = [[[UIImage imageNamed:@"red.png"] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0f] retain];

altGreen = [[[UIImage imageNamed:@"green2.png"] stretchableImageWithLeftCapWidth:capWidth topCapHeight: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];


}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马