i am using CGContextRef
to draw one horizontal bar and above-below of bar i am displaying text using drawInRect
method of NSString
.Here is code:
CGContextRef context = UIGraphicsGetCurrentContext();//set frame for bar
CGRect frame;
frame.origin.x = prevWidth;
frame.origin.y = heightBar;
frame.size.height = heightOfBar;
frame.size.width = 10;
UIColor* color = [ColorArray objectAtIndex:i];
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextSetLineWidth(context, lineWidth);
CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
CGContextFillRect(context, frame);
CGContextStrokeRect(context, frame);
[nameString drawInRect:frame1 withFont:font lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter];
When i call second time it gives me error like:<Error>: CGContextSetFillColorWithColor: invalid context 0x0
.Please help me.Thanking you.
The error says the context is invalid .So i suppose calling the second time means you are calling the method directly i guess.
As per docs
The current graphics context is nil by default. Prior to calling its drawRect: method, view objects push a valid context onto the stack, making it current.
The context will be valid only in drawRect:
method.You need to call -setNeedsDisplay
on the UIView to update the contents