核心图形绘制一条线中带有边框的矩形



如何在一条线上画一个有边框的矩形?

有单独的方法,例如:

CGContextStrokeRect(context, someRectangle);

CGContextFillRect(context, someRectangle);

但是有什么东西可以同时做到这两点吗?

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPathRef path = CGPathCreateWithRect(rect, NULL);
    [[UIColor redColor] setFill];
    [[UIColor greenColor] setStroke];
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathFillStroke);
    CGPathRelease(path);
}

虽然,我不能说它比笔画和填写单独的调用更冗长......

如果您只是想节省行空间,则可以定义自己的方法来进行两次调用并将其放在实用程序类中。

void strokeAndFill(CGContextRef c, CGRect rect)
{
    CGContextFillRect(c, rect);
    CGContextStrokeRect(c, rect);
}

如果您事先设置填充和描边颜色,CGContextDrawPath可以一次性完成。

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColor(context, a);
CGContextSetFillColor(context, b);
CGContextDrawPath(context, rect)

最新更新