如何一行代码画一个带边框的矩形?
有多种方法,比如:
CGContextStrokeRect(context, someRectangle);
并且
CGContextFillRect(context, someRectangle);
但是有没有一种同时实现这两个功能的东西呢?
如何一行代码画一个带边框的矩形?
有多种方法,比如:
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)
CGContextDrawPath
的签名是:CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode)
。 - chinabuffet