【发布时间】:2012-12-04 19:13:24
【问题描述】:
如何在一行中绘制一个有边框的矩形?
有不同的方法,例如:
CGContextStrokeRect(context, someRectangle);
和
CGContextFillRect(context, someRectangle);
但是有什么东西可以同时做吗?
【问题讨论】:
标签: objective-c ios core-graphics
如何在一行中绘制一个有边框的矩形?
有不同的方法,例如:
CGContextStrokeRect(context, someRectangle);
和
CGContextFillRect(context, someRectangle);
但是有什么东西可以同时做吗?
【问题讨论】:
标签: objective-c ios core-graphics
- (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)