Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate CGContext for CoreText
    primarykey
    data
    text
    <p>I was trying to render some CoreText in a CALayer, so that i can adjust the alpha of that layer explicitly. I tried overriding the drawRect: method but that, as far as i know, only draws on the root layer of the view, and i have two sublayers, a bg image and the CoreText. Here's the method that attempts to draw the CoreText layer: (which works in drawRect if i remove the createContext code)</p> <pre><code>-(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, &amp;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), &amp;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]; } </code></pre> <p>--any theory on CALayers and CGContexts is much appreciated </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload