jpsasi
jpsasi

Reputation: 1905

iPhone How to set a clearcolor for my custom view

I am creating a custom StyleLabel by extending the UIView, which provides a link for particular type of strings. I have successfully implemented this functionality.

I am using this label in TableViewCell along with UILabel.i set UILabel & StyleLabel background color is set to clearColor. UILabel works fine, but StyleLabel shows background in blackColor.

I am not sure what is the problem with the following code.

Here is the drawRect method of CustomLabel class

-(void) drawRect:(CGRect)rect 
{
     CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextClearRect(ctx, rect);
    [[UIColor clearColor] setFill];
    CGContextAddRect(ctx, rect);
    CGContextDrawPath(ctx, kCGPathFill);
    [_text drawText:ctx];

}

Thanks Sasikumar

Upvotes: 0

Views: 2208

Answers (1)

drawnonward
drawnonward

Reputation: 53669

If a view is opaque, then clearColor will draw as black. Be sure to set theView.opaque = NO for any view that should be clear or transparent.

This code is not needed after CGContextClearRect:

[[UIColor clearColor] setFill];
CGContextAddRect(ctx, rect);
CGContextDrawPath(ctx, kCGPathFill);

Upvotes: 7

Related Questions