Core Text View has black background color
Asked Answered
E

1

6


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

Earlie answered 15/2, 2011 at 15:9 Comment(0)
Z
14

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.

Zimbabwe answered 15/2, 2011 at 15:32 Comment(1)
Yes, the solution was so easy that I didn't try it :) I just needed to set setBackgroundColor:[UIColor clearColor] when the view is created in the viewController class. thank you anyway.Earlie

© 2022 - 2024 — McMap. All rights reserved.