【问题标题】:Changing a fill color on ellipse using Objective-C使用 Objective-C 更改椭圆的填充颜色
【发布时间】:2015-04-26 23:40:51
【问题描述】:

我正在重写 drawRect 方法以绘制椭圆

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2.0);

CGContextSetStrokeColorWithColor(context, [UIColor mainColorYellow].CGColor);

CGRect rectangle = CGRectMake(0.0, 2.0, rect.size.width , rect.size.height - 4.0);

CGContextAddEllipseInRect(context, rectangle);

CGContextStrokePath(context);

}

如何更改椭圆的填充颜色?我尝试使用 CGContextSetFillColorWithColor(CGContextRef c, CGColorRef color) 但没有结果。

【问题讨论】:

  • 你只是在抚摸路径。你需要填写它。

标签: ios core-graphics


【解决方案1】:

有两种不同的操作,填充和描边。描边是边界,填充是内部。你只是在做边界。您可以独立控制填充和边框颜色。但是,您必须显式地渲染两者。例如:

[[UIColor yellowColor] setFill];
[[UIColor blackColor] setStroke];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0.0, 2.0, rect.size.width , rect.size.height - 4.0)];
path.lineWidth  = 2.0;        
[path fill];
[path stroke];

请注意,我用更现代的石英编写了代码。上下文是隐含的。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    只需在下面添加:

        CGContextFillEllipseInRect(context, rectangle);
    

    【讨论】: