iOS——在tableViewCell中画线虚线


在创建cell的方法中添加下面的代码就可以在cell中添加虚线了,效果如图

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:cell.contentView.bounds];
    [shapeLayer setPosition:cell.contentView.center];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    
    [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
    [shapeLayer setStrokeColor:[[UIColor lightGrayColor] CGColor]];
    
    [shapeLayer setLineWidth:0.3f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:2], nil]];
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
//这两个是设置横线两端的起始位置
    CGPathMoveToPoint(path, NULL, 0, 35);
    CGPathAddLineToPoint(path, NULL, width, 35);
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    [[cell.contentView layer] addSublayer:shapeLayer];


你可能感兴趣的:(iOS——在tableViewCell中画线虚线)