UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom]
signBtn.frame = CGRectMake(0, 0, 80, 40)
[signBtn.layer setMasksToBounds:YES]
[signBtn.layer setCornerRadius:10.0]
//设置矩形四个圆角半径
[signBtn.layer setBorderWidth:1.0]
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB()
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 })
[signBtn.layer setBorderColor:colorref]
//边框颜色.
看一下效果图,类似于淘宝加入购物车,我用的是UIButton
颜色颜色Button和型号一样:
1.创建UIButton
for (int i = 0i <6i++) {
self.selectColorBtn = [UIButton buttonWithType:UIButtonTypeCustom]
if (i >4) {
self.selectColorBtn.frame = CGRectMake(70 * widthScale + 60 * widthScale * (i-5) , 50 * widthScale, 50 * widthScale, 30 *widthScale)
}else{
self.selectColorBtn.frame = CGRectMake(70 * widthScale + 60 * widthScale * i , 10 * widthScale, 50 * widthScale, 30 *widthScale)
}
[self.selectColorBtn setTitle:@"暗灰色" forState:UIControlStateNormal]
[self.selectColorBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]
self.selectColorBtn.tag = 100 + i
self.selectColorBtn.titleLabel.font = [UIFont systemFontOfSize:15 * widthScale]
[self.selectColorBtn.layer setBorderWidth:1.0]
[self.selectColorBtn.layer setBorderColor:[gray CGColor]]
[self.selectColorBtn addTarget:self action:@selector(selectColorAction:) forControlEvents:UIControlEventTouchUpInside]
[self.twoView addSubview:self.selectColorBtn]
}
2.按钮点击事件
- (void)selectColorAction:(UIButton *)btn{
//将Button颜色全部修改为灰色
for (int i = 0i <6i++) {
//通过tag值获取UIbutton
UIButton *btn = (UIButton *)[self.twoView viewWithTag:100 + i]
[btn.layer setBorderColor:[gray CGColor]]
}
//获取当前UIButton
UIButton *nowBtn = btn
switch (btn.tag) {
case 100:
[nowBtn.layer setBorderColor:[ThemeColors CGColor]]
break
case 101:
[nowBtn.layer setBorderColor:[ThemeColors CGColor]]
break
case 102:
[nowBtn.layer setBorderColor:[ThemeColors CGColor]]
break
case 103:
[nowBtn.layer setBorderColor:[ThemeColors CGColor]]
break
case 104:
[nowBtn.layer setBorderColor:[ThemeColors CGColor]]
break
case 105:
[nowBtn.layer setBorderColor:[ThemeColors CGColor]]
break
default:
break
}
UIButton *button = [UIButton alloc] initWithFrame:CGRect(100, 100, 100 , 100)]//设置边框颜色
button.layer.borderColor = [[UIColor redColor] CGColor]
//设置边框宽度
button.layer.borderWidth = 1.0f
//给按钮设置角的弧度
button.layer.cornerRadius = 4.0f
//设置背景颜色
button.backgroundColor = [UIColor redColor]
button.layer.masksToBounds = YES
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)