Take the 2-minute tour ×

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 =
        CFRangeMake(0, [attString length]), path, NULL);

CTFrameDraw(frame, context); //4

CFRelease(frame); //5

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?

share|improve this question
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 6: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 6: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 7:03

2 Answers 2

up vote 0 down vote accepted

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

share|improve this answer
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
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
Actually nothing, except that way the text is displayed.. –  Lolloz89 May 24 '12 at 17:22

Try setting the background colour to transparent

 viewName.backgroundColor = [UIColor clearColor];
share|improve this answer

Your Answer


