UIColor SetFill doesn't work

匆匆过客 提交于 2019-12-02 07:30:28

问题


In this code

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);

Line with setFill method doesn't work. What might be the problem of this? Code is located in drawRect: method


回答1:


setFill isn't for Core Graphics drawing but for drawing like [myUIBezierPath fill];

Instead set the fill color and stroke color using:

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

Also, the following line:

CGContextDrawPath(context, kCGPathStroke);

Will only stroke the path, since the drawing mode is set to kCGPathStoke. To fill it as well you should replace it with

CGContextDrawPath(context, kCGPathFillStroke);

If your path has holes in it or crosses itself you should use even-odd fill and stroke

CGContextDrawPath(context, kCGPathEOFillStroke);



回答2:


Not only do you need to call the appropriate CG* methods to set the fill like David suggested, you need to actually perform the fill using the following after you set the fill and stroke properties:

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


来源:https://stackoverflow.com/questions/10623545/uicolor-setfill-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!