UIColor SetFill不起作用

在这个代码中

for (int i=0;i<3;i++) { CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*(i+1), self.yShift+self.rectLen*10); CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*(i+1), self.yShift+self.rectLen*10+self.rectLen); CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*i, self.yShift+self.rectLen*10+self.rectLen); CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*i, self.yShift+self.rectLen*10); CGContextMoveToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*i, self.yShift+self.rectLen*10); } CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*4, self.yShift+self.rectLen*10); CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*4, self.yShift+self.rectLen*10+self.rectLen); CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*3, self.yShift+self.rectLen*10+self.rectLen); CGContextAddLineToPoint(context, self.xShift+15+self.rectLen*self.width+self.rectLen*3, self.yShift+self.rectLen*10); [[UIColor cyanColor] setFill]; [[UIColor blackColor] setStroke]; CGContextSetLineWidth(context, 1); CGContextDrawPath(context, kCGPathStroke); 

用setFill方法行不起作用。 这可能是什么问题? 代码位于drawRect:方法中

setFill不是用于Core Graphics绘图,而是用于绘制[myUIBezierPath fill];

相反,请使用以下设置填充颜色和描边颜色:

 CGContextSetFillColorWithColor(context, [[UIColor cyanColor] CGColor]); CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); 

另外,下面一行:

 CGContextDrawPath(context, kCGPathStroke); 

由于绘图模式设置为kCGPathStoke,因此只能绘制path。 为了填补它,你应该replace它

 CGContextDrawPath(context, kCGPathFillStroke); 

如果你的path有洞或穿越自己,你应该使用偶数填充和中风

 CGContextDrawPath(context, kCGPathEOFillStroke); 

您不仅需要调用相应的CG*方法来设置像David所build议的填充,您需要在设置填充和描边属性后使用以下方法实际执行填充:

 CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor); CGContextSetStrokeColorWithColor(context, [UIColor blackColor.CGColor); CGContextFillPath(context); CGContextStrokePath(context);