Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Text View has black background color


I'm having a UIView that renders some text using CoreText,everything works fine, except for the fact that the entire view has black background color. I've tried the most basic solutions like

[self setBackgroundColor:[UIColor clearColor]];

or

CGFloat components[] = {0.f, 0.f, 0.f, 0.f };
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef transparent = CGColorCreate(rgbColorSpace, components);
CGContextSetFillColorWithColor(context, transparent);
CFRelease(transparent);

but this didn't help at all.
I'm just pounding my head on this issue, because I can't quite understand how does really core text works and how is this pure-C layer related with all the other objective-c stuff. And this doesn't help very much in solving this problem, so I'm asking you folks, if could you provide me a solution and (most important) an explanation about it.

FIY I'm posting also the code of the CoreText view.
it's all based on 2 functions: parseText which builds up the CFAttributedString and the drawRect that takes care of the drawing part.

-(void) parseText {
    NSLog(@"rendering this text: %@", symbolHTML);
    attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef)symbolHTML);

    CFStringRef fontName = (CFStringRef)@"MetaSerif-Book-Accent";
    CTFontRef myFont = CTFontCreateWithName((CFStringRef)fontName, 18.f, NULL); 
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = { 1.0, 0.3f, 0.3f, 1.f };
    CGColorRef redd = CGColorCreate(rgbColorSpace, components);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0,0),
                                   kCTForegroundColorAttributeName, redd);
    CFRelease(redd);

    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFStringGetLength((CFStringRef)symbolHTML)), kCTFontAttributeName, myFont);



    CTTextAlignment alignment = kCTLeftTextAlignment;
    CTParagraphStyleSetting settings[] = {
        {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}
    };
    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFStringGetLength((CFStringRef)attrString)), kCTParagraphStyleAttributeName, paragraphStyle);

    [self calculateHeight];
}

and this is the DrawRect function:

- (void)drawRect:(CGRect)rect {
    /* get the context */
    CGContextRef context = UIGraphicsGetCurrentContext();    


    /* flip the coordinate system */    

    float viewHeight = self.bounds.size.height;
    CGContextTranslateCTM(context, 0, viewHeight);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, 1.0));


    /* generate the path for the text */
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect bounds = CGRectMake(0, 0, self.bounds.size.width-20.0, self.bounds.size.height);
    CGPathAddRect(path, NULL, bounds);    


    /* draw the text */
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
                                                CFRangeMake(0, 0), path, NULL);
    CFRelease(framesetter);
    CFRelease(path);
    CTFrameDraw(frame, context);
    CGFloat components[] = {0.f, 0.f, 0.f, 0.f };
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef transparent = CGColorCreate(rgbColorSpace, components);
    CGContextSetFillColorWithColor(context, transparent);
    CFRelease(transparent);
}

I hope that everything is clear, but if something looks confusing, just ask and I'll be glad to explain myself better.

thank you in advance for this hardcore issue :)
k

like image 224
holographix Avatar asked Feb 15 '11 15:02

holographix


1 Answers

Here's a question. Did you remember to set view.opaque = NO? Because if not you'll get that black background regardless of what else you do.

like image 196
Nicholas1024 Avatar answered Oct 11 '22 11:10

Nicholas1024