Summary of IOS's method for drawing dotted lines
- 2020-09-16 07:48:25
- OfStack
1. Rewrite drawRect method.
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Set the dotted line color
CGContextSetStrokeColorWithColor(currentContext, [UIColor BlackColor].CGColor);
// Set the dotted line width
CGContextSetLineWidth(currentContext, 1);
// Sets the starting point for the dotted line drawing
CGContextMoveToPoint(currentContext, 0, 0);
// Set the dotted line to draw the endpoint
CGContextAddLineToPoint(currentContext, self.frame.origin.x + self.frame.size.width, 0);
// Sets the width interval of the dotted line arrangement : The following arr The number in represents draw first 3 I'm going to draw a dot 1 A point
CGFloat arr[] = {3,1};
// Below the last 1 A parameter" 2 "Is the number of permutations.
CGContextSetLineDash(currentContext, 0, arr, 2);
CGContextDrawPath(currentContext, kCGPathStroke);
}
2. Use CAShapeLayer to draw dotted lines
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:self.bounds];
[shapeLayer setPosition:CGPointMake(self.frame.size.width / 2.0, self.frame.size.height)];
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// Set the dotted line color
shapeLayer setStrokeColor:[UIColor BlackColor].CGColor];
// Set the dotted line width
[shapeLayer setLineWidth:self.frame.size.height];
[shapeLayer setLineJoin:kCALineJoinRound];
// Set the line width and spacing of the dotted lines
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:3], [NSNumber numberWithInt:1], nil]];
// Create a dotted draw path
CGMutablePathRef path = CGPathCreateMutable();
// Sets the dotted line to draw the starting point of the path
CGPathMoveToPoint(path, NULL, 0, 0);
// Sets the dashed line to draw the path end point
CGPathAddLineToPoint(path, NULL, self.frame.size.width, 0);
// Sets the dotted line drawing path
[shapeLayer setPath:path];
CGPathRelease(path);
// Add the dotted line
[self.layer addSublayer:shapeLayer];
About this way has been sorted out a very good class method, specific see the code below, note: the following is not complete code, if there is a need, please search their Own Baidu.
/**
** lineView: I need to draw it as a dotted line view
** lineLength: The width of the dotted line
** lineSpacing: Spacing of dotted lines
** lineColor: The color of the dotted line
**/
+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
.....
[shapeLayer setStrokeColor:lineColor.CGColor];
......
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
......
[lineView.layer addSublayer:shapeLayer];
}
3. Economical: Use mapping to draw dotted lines (designers are required to cooperate with cutting drawings)
UIImageView *imgDashLineView =[[UIImageView alloc] initWithFrame:CGRectMake(15, 200, self.view.frame.size.width - 30, 1)];
[imgDashLineView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"xuxian.png"]]];
[self.view addSubview:imgDashLineView];
conclusion
Part of the above content is from the network, in the spirit of sharing learning, if there is any infringement, please timely inform. Above is the whole content of this article, welcome everyone 1 discussion study, have a question please leave a message, this site will reply to your question as soon as possible.