iOS OC 高效圆角、圆角边框色

开发中常用到圆角,圆角边框色等,现在记录一种高效绘制方式:

主要用到类有:UIBezierPath、CAShapeLayer

仅仅圆角,直接上代码:

UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake([Adapter adapterWidthBy6s:30], 0, [Adapter adapterWidthBy6s:200], [Adapter adapterWidthBy6s:200])];  UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:img.bounds cornerRadius:3.f];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    maskLayer.frame =

;

    maskLayer.path = maskPath.CGPath;

    img.layer.mask = maskLayer;

此处比较简单,不再附截图,具体效果,可直接粘贴代码查看效果

圆角+边框色:

    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake([Adapter adapterWidthBy6s:30], 0, [Adapter adapterWidthBy6s:200], [Adapter adapterWidthBy6s:200])];
    //圆角+边框色
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = img.bounds;
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.frame = img.bounds;
    borderLayer.lineWidth = 1.f;
    borderLayer.strokeColor = [UIColor textf0f3f5Color].CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    UIBezierPath *bezierPath =  [UIBezierPath bezierPathWithRoundedRect:img.bounds cornerRadius:3.f];
    maskLayer.path = bezierPath.CGPath;
    borderLayer.path = bezierPath.CGPath;
    [img.layer insertSublayer:borderLayer atIndex:0];
    [img.layer setMask:maskLayer];


使用Masonry 时,在创建view 时需要设置frame,也可以直接创建CGRect,但要保证size与masonry设置的size相同。


效果图

你可能感兴趣的:(iOS OC 高效圆角、圆角边框色)