iOS笔记之UIKit_UIButton
//UIButton的基本属性
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(0, 200, 90, 90);
_btn.backgroundColor = [UIColor redColor];
_btn.tag = 100;
[_btn setTitle:@"我爱你" forState:UIControlStateNormal ];
[_btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal ];
[_btn addTarget:self action:@selector(btnClick:) forControlEvents: UIControlEventTouchUpInside];
//设置button的圆角、边框
_btn.layer.cornerRadius =10;
_btn.layer.borderWidth = 5.0;
_btn.layer.borderColor = [UIColor blueColor].CGColor;
//设置button标签文字的颜色
[_btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
//标签文字的字体
[_btn.titleLabel setFont:[UIFont systemFontOfSize:28]];
_btn.tag = 101;
[self.view addSubview:_btn];
UIButton*bnt = [UIButton buttonWithType:UIButtonTypeSystem];
bnt.frame = CGRectMake(60, 300, 80, 80);
//button的颜色
bnt.backgroundColor = [UIColor grayColor];
bnt.layer.cornerRadius = 40;
bnt.layer.borderColor = [UIColor redColor].CGColor;
bnt.layer.borderWidth = 5.0;
[bnt setTitle:@"mapanguan" forState:UIControlStateNormal ];
[bnt setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal ];
[bnt.titleLabel setFont:[UIFont systemFontOfSize:20]];
bnt.tag = 102;
//添加点击事件
[bnt addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDragOutside];
[self.view addSubview:bnt];
}
-(void)btnClick:(UIButton*)btn{
if (101 == _btn.tag) {
NSLog(@"按钮被点击了");
//点击显示随机颜色(需先定义)
int index = arc4random()%[self.colors count];
self.btn.backgroundColor = self.colors[index];
}
}