UIButton设置圆角和边框颜色

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:btn];
    
    // 设置曲线
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(50, 50)];
    // 设置蒙层
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = btn.bounds;
    layer.path = path.CGPath;
    btn.layer.mask = layer;
    // 设置一个和蒙层大小相同的layer,设置颜色和宽度,添加到button上
    CAShapeLayer *boderLayer = [CAShapeLayer layer];
    boderLayer.frame = btn.bounds;
    boderLayer.path = path.CGPath;
    boderLayer.strokeColor = [UIColor greenColor].CGColor;
    boderLayer.fillColor = [UIColor clearColor].CGColor;
    boderLayer.lineWidth = 1.f;
    [btn.layer addSublayer:boderLayer];
posted @ 2024-09-03 15:38  可可锋  阅读(89)  评论(0)    收藏  举报