美文网首页
Swift UIButton 圆角

Swift UIButton 圆角

作者: ping_oO | 来源:发表于2017-04-22 14:03 被阅读761次

<code>//有离屏渲染</code>

    let button = UIButton(frame: CGRect(x: 100, y: 100, width: 60, height: 50))
    button.backgroundColor = UIColor.brown
    button.setTitle("test", for: .normal)
    view.addSubview(button)

    let bezierPath = UIBezierPath(roundedRect: button.bounds, 
                            byRoundingCorners: [.allCorners], //哪个角
                                  cornerRadii: CGSize(width: 5, height: 5)) //圆角半径
    let maskLayer = CAShapeLayer()
    maskLayer.path = bezierPath.cgPath
    button.layer.mask = maskLayer;

<code>//如果没有图片想设置圆角,可以直接设置</code>

    button.layer.cornerRadius = radius 

<code>//但不要设置</code>

      button.layer.masksToBounds = true 
或    button.clipsToBounds = true

<code>//不然也会有有离屏渲染</code>

效果图


0301E059-5780-4487-87B1-22F161F45BCE.png

UILabel 设置圆角

label.text = @"1";
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:12];
label.layer.backgroundColor = [UIColor greenColor].CGColor;
label.layer.cornerRadius = 2;

相关文章

网友评论

      本文标题:Swift UIButton 圆角

      本文链接:https://www.haomeiwen.com/subject/raedzttx.html