0

This is my drawRect method.

- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();

CGMutablePathRef path = CGPathCreateMutable(); //1
CGPathAddRect(path, NULL, self.bounds );

NSAttributedString* attString = [[[NSAttributedString alloc]
    initWithString:@"Hello core text world!"] autorelease]; //2

CTFramesetterRef framesetter =
    CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); //3
CTFrameRef frame =
    CTFramesetterCreateFrame(framesetter,
        CFRangeMake(0, [attString length]), path, NULL);

CTFrameDraw(frame, context); //4

CFRelease(frame); //5
CFRelease(path);
CFRelease(framesetter);
}

Once I load this view in my view controller, the window become black. If I comment the drawRect method, everything go ok.. Where is the mistake?

Lolloz89
  • 2,809
  • 2
  • 26
  • 41
  • You shouldn't need to call [super drawRect:rect]; since "The default implementation of this method does nothing" – David Rönnqvist May 21 '12 at 21:44
  • I tried, nothing changed. Thanks anyway.. – Lolloz89 May 22 '12 at 06:36
  • The docs for CTFrameDraw says: "This call can leave the context in any state and does not flush it after the draw operation." Have you tried inserting `CGContextFlush(context);` after CTFrameDraw? It "Forces all pending drawing operations". – David Rönnqvist May 22 '12 at 06:44
  • Something strange happened. If i load this view from a nib, everything works fine... Has anyone some ideas why? – Lolloz89 May 22 '12 at 07:03

3 Answers3

0

I solved this problem creating a viewController with my custom uiview inside. Not really what I wanted, but it works..

Lolloz89
  • 2,809
  • 2
  • 26
  • 41
  • Hang on, you had `drawRect` in your view controller? If so, then yes, that won't work! `drawRect` is a method of `UIView`. – mattjgalloway May 23 '12 at 20:27
  • 1
    Oh no, the drawRect method was in the UIView! That would be a very stupid question to ask ;) – Lolloz89 May 24 '12 at 16:12
  • Then why did creating another view controller with the custom view inside work? How did it differ from what you originally did? – mattjgalloway May 24 '12 at 17:05
0

Try setting the background colour to transparent

 viewName.backgroundColor = [UIColor clearColor];
joshuahornby10
  • 4,222
  • 8
  • 36
  • 52
0

I had the same issue. Was attempting to draw simple circles. Created a simple UIView subclass, overrode drawRect and there was always a black square rendered behind the circles.

self.backgroundColor = [UIColor clearColor];

in the init fixed it!

smottt
  • 3,272
  • 11
  • 37
  • 44
zecmo
  • 46
  • 4