2012-12-04 42 views

回答

9
- (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); 
} 

虽然,我不能说这是比任何行程&填写单独的呼叫更简洁...

+0

嗯......很真实,我只是去与单独的呼叫我想。谢谢。 – chinabuffet

-1

如果您事先设置填充和描边颜色,CGContextDrawPath会一举成功。

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

嗯...... CGContextDrawPath'的'我在头文件中看到签名是:'CGContextDrawPath(CGContextRef C,CGPathDrawingMode模式)' – chinabuffet

6

如果你只是希望节省行距,你可以定义你的自己的方法来进行两个调用并将其放置在实用程序类中。

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