美文网首页零碎知识点iOS
iOS为UIView添加虚线边框

iOS为UIView添加虚线边框

作者: 蓝色的雪啦 | 来源:发表于2016-11-28 13:26 被阅读50次

有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShapeLayer来完成。效果图如下:


CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat    viewWidth = 200;CGFloat viewHeight = 200;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake((screenSize.width - viewWidth)/2, (screenSize.height - viewHeight) / 2, viewWidth, viewHeight)];
view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
view.layer.cornerRadius = CGRectGetWidth(view.bounds)/2;
CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.bounds = CGRectMake(0, 0, viewWidth, viewHeight);
borderLayer.position = CGPointMake(CGRectGetMidX(view.bounds), CGRectGetMidY(view.bounds));// 
borderLayer.path = [UIBezierPath bezierPathWithRect:borderLayer.bounds].CGPath;
borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:CGRectGetWidth(borderLayer.bounds)/2].CGPath;borderLayer.lineWidth = 1. / [[UIScreen mainScreen] scale];
//虚线边框borderLayer.lineDashPattern = @[@8, @8];
//实线边框// borderLayer.lineDashPattern = nil;
borderLayer.fillColor = [UIColor clearColor].CGColor;
borderLayer.strokeColor = [UIColor redColor].CGColor;
[view.layer addSublayer:borderLayer];
[self.view addSubview:view];

可通过修改UIBezierPath来改变虚线框的路径。如果想把边框绘制成实线,可将borderLayer.lineDashPattern
置为nil
即可。

相关文章

  • iOS为UIView添加虚线边框

    有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShap...

  • iOS为UIView添加虚线边框

    有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShap...

  • 为 UIView 添加虚线边框

    可通过修改UIBezierPath来改变虚线框的路径。

  • ios关于UI

    1.ios在控件上添加虚线边框 内容很详细,涉及到虚线边框圆角问题及解决过程。 2.ios截图 ①截某个显示图片的...

  • 给UIView添加带圆角的完美虚线框

    iOS autoLayout约束下,给UITableViewCell内部的UIView添加带圆角虚线框

  • iOS view画圆角虚线

    #pragma mark 虚线边框 - (void)addBorderToLayer:(UIView *)view...

  • 给控件添加边框

    经常会遇到给控件添加边框的需求:边框大体有两种实线和虚线 1、虚线边框的添加: 2、实现边框的添加: a、最常采用...

  • UIView 虚线边框

    #import@interface DashesLineView : UIView @property(nonat...

  • iOS view添加虚线边框

    有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShap...

  • iOS view添加虚线边框

    有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShap...

网友评论

    本文标题:iOS为UIView添加虚线边框

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