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相同。


效果图
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容