2011-02-15 49 views
5


我有一个使用CoreText渲染一些文本的UIView,一切正常, 除了整个视图具有黑色背景色这一事实。 我试过最基础的解决方案,如核心文本视图有黑色背景颜色

[self setBackgroundColor:[UIColor clearColor]]; 

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

但这并没有帮助的。
我只是在这个问题上冲击我的脑海,因为我不太明白 真正的核心文本是如何工作的,以及这个纯粹的C层如何与其他所有Objective-C相关的 相关。 这对解决这个问题没有太大的帮助,所以我问你们,如果你能给我一个解决方案和(最重要的)关于它的解释。

FIY我也发布了CoreText视图的代码。
它都基于2个功能:parseText其中构建CFAttributedString和 drawRect负责绘图部分。

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

,这是的drawRect功能:

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

我希望一切都清楚了,但如果事情看起来 混乱,只是问,我会很高兴,以更好地解释一下。

感谢你在这个铁杆问题:)
ķ

回答

10

这里有一个问题。你记得设置view.opaque = NO吗?因为如果没有,你会得到那个黑色背景,不管你做了什么。

+0

是的,解决方案非常简单,我没有尝试它:)我只需要在viewController类中创建视图时设置`setBackgroundColor:[UIColor clearColor]`。不管怎样,谢谢你。 – holographix 2011-02-15 15:34:28