UIView *View = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 100, 100))];
View.center = self.view.center;
[self.view addSubview:View];
View.backgroundColor = [UIColor grayColor];
一、实线边框
实线边框.png
View.layer.borderColor = [UIColor redColor].CGColor;
View.layer.borderWidth = 1;
二、虚线边框
1.虚线边框主要实现是通过增加一个layer绘制一个虚线的矩形,lineDashPattern 第一个参数代表线段长度,第二个参数代表线段间距。
CAShapeLayer *dottedLineBorder = [[CAShapeLayer alloc] init];
dottedLineBorder.frame = CGRectMake(0, 0, View.frame.size.width, View.frame.size.height);
[dottedLineBorder setLineWidth:2];
[dottedLineBorder setStrokeColor:[UIColor redColor].CGColor];
[dottedLineBorder setFillColor:[UIColor clearColor].CGColor];
dottedLineBorder.lineDashPattern = @[@10,@20];//10 - 线段长度