Changing a fill color on ellipse using Objective-C

时间:2015-04-26 23:40:52

标签: ios core-graphics

I am overriding drawRect method in order to draw ellipse

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

}

How can I change fill color on ellipse? I tried using CGContextSetFillColorWithColor(CGContextRef c, CGColorRef color) but without result.

2 个答案:

答案 0 :(得分:4)

有两种不同的操作,填充和描边。中风边界,填充是内部。你只是做边界。您可以单独控制填充和边框颜色。但是,您必须明确地呈现这两者。例如:

[[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];

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

答案 1 :(得分:0)

只需添加以下内容:

    CGContextFillEllipseInRect(context, rectangle);