UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame=CGRectMake(100, 100, 200, 100) ;
btn1.backgroundColor=[UIColor yellowColor];
[btn1 setTitle:@"抬起" forState:UIControlStateNormal];
btn1.titleLabel.font=[UIFont systemFontOfSize:30.f];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn1 setImage:[UIImage imageNamed:@"2"] forState:UIControlStateHighlighted];
[btn1 setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
[btn1 setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn1.titleLabel.shadowOffset=CGSizeMake(1, 2);
btn1.imageView.backgroundColor=[UIColor blackColor];//设置按钮的图片控件的背景颜色为黑色
target是执行事件的对象,一般是Controller对象
action是处理事件的方法
forControlEvents 是事件类型:
例如下面代码,
target处理的对象是ViewController类对象,这里是self,
action处理方法是:clickBtn: 注意:冒号不能省略,因为下面后面带参数,就要加:冒号
forControlEvents处理事件是鼠标点击UIControlEventTouchUpInside
- (void)viewDidLoad {
//....
[btn1 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickBtn:(UIButton*)sender {
[sender setTitle:@"抬起" forState: UIControlStateNormal];
[sender setBackgroundColor:[UIColor redColor]];
}
NSString *strMsg=[_btn1 titleForState:UIControlStateNormal];
UIColor *uc1=[_btn1 titleColorForState:UIControlStateNormal];
UIImage * im1=[_btn1 imageForState:UIControlStateNormal];
UIImage *bg1=[_btn1 backgroundImageForState:UIControlStateNormal];