【问题标题】:Create CGContext for CoreText为 CoreText 创建 CGContext
【发布时间】:2012-05-15 17:12:11
【问题描述】:

我试图在 CALayer 中渲染一些 CoreText,以便我可以显式调整该层的 alpha。我尝试覆盖 drawRect: 方法,但据我所知,它只在视图的根层上绘制,并且我有两个子层,一个 bg 图像和 CoreText。这是尝试绘制 CoreText 层的方法:(如果我删除 createContext 代码,它在 drawRect 中有效)

-(void) drawTextLayer
{
    //get context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(250.0, 137.0), YES, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();

//draw text
//attribute string
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:displayTxt];

//get font    
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"idolwild"
                                                     ofType:@"ttf"];

CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)fontPath, kCFURLPOSIXPathStyle, false);
CGDataProviderRef dataProvider = CGDataProviderCreateWithURL(url);
CGFontRef theCGFont = CGFontCreateWithDataProvider(dataProvider);
CTFontRef idolwild = CTFontCreateWithGraphicsFont(theCGFont, 16.0, &CGAffineTransformIdentity, nil);

CFRelease(theCGFont);
CFRelease(dataProvider);
CFRelease(url);

[attrStr addAttribute:(id)kCTFontAttributeName
                value:(id)idolwild
                range:NSMakeRange(0, [attrStr length])];


//create paragraph style and assign text alignment to it
CTTextAlignment alignment = kCTCenterTextAlignment;
CTParagraphStyleSetting _settings[] = {    {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment} };
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings) / sizeof(_settings[0]));

//set paragraph style attribute
[attrStr addAttribute:(id)kCTParagraphStyleAttributeName
                value:(id)paragraphStyle
                range:NSMakeRange(0, [attrStr length])];


[attrStr addAttribute:(id)kCTForegroundColorAttributeName
                value:(id)[UIColor clearColor].CGColor
                range:NSMakeRange(0, [attrStr length])];


[attrStr addAttribute:(id)kCTForegroundColorAttributeName
                value:(id)[UIColor blackColor].CGColor
                range:NSMakeRange(0, [[wordRanges objectAtIndex:currentTextindex] intValue])];


//layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrStr);

//bounding eclipse
CGMutablePathRef boundingEclipse = CGPathCreateMutable();
CGPathAddEllipseInRect(boundingEclipse, NULL, boundingEclipseRect);


//ctframe
CTFrameRef bubbleFrame = CTFramesetterCreateFrame(framesetter, 
                                                  CFRangeMake(0, 0),
                                                  boundingEclipse, NULL);

// flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);


    //fill bg to visual
    CGContextAddPath(context, boundingEclipse);
    [[UIColor greenColor] setFill];
    CGContextFillPath(context);

//draw
CTFrameDraw(bubbleFrame, context);

//add to layer
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
textLayer.contents = tempImage;

//cleanup
CGPathRelease(boundingEclipse);
CFRelease(framesetter);
CFRelease(bubbleFrame);
CFRelease(idolwild);
CGContextRelease(context);

[attrStr release];
}

--任何关于 CALayers 和 CGContexts 的理论都非常感谢

【问题讨论】:

    标签: ios calayer cgcontext core-text


    【解决方案1】:

    我可以建议三种不同的方法:

    1. 子类CALayer并实现其drawInContext:

    2. 将不是UIView的对象设置为子层的delegate,然后实现drawLayer:inContext:

    3. 创建一个CGImage 并将其设置为图层的内容:

      UIGraphicsBeginImageContext(size);
      CGContextRef context = UIGraphicsGetCurrentContext();
      
      /// draw
      
      UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      
      sublayer.contents = (id)image.CGImage;
      

    【讨论】:

    • 我喜欢建议#3。关于前两个建议的一个问题,drawInContext: 方法,是将 CALayer 作为图像绘制到 Context 中,还是使用 param 上下文绘制 CALayer?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2022-08-22
    • 1970-01-01
    相关资源
    最近更新 更多