The main text view has a black background color


I have a UIView that displays some text using CoreText, everything works fine, except for the fact that the whole view has a black background color. I 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 it did not help. I am just banging my head on this issue because I cannot understand how the main text works and how this clean C layer with all other objective-c things. And it doesn’t really help in solving this problem, so I ask you if you would provide me with a solution and (most important) explanation about it.

FIY I also send CoreText view code.
all of this is based on two functions: parseText, which creates a CFAttributedString, and drawRect, which takes care of part of the drawing.

-(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);
}

, , - , , .

:)

+5
1

. view.opaque = NO? , , , .

+10

All Articles