Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7954225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:21:52+00:00 2026-06-04T03:21:52+00:00

I was trying to render some CoreText in a CALayer, so that i can

  • 0

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)

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

–any theory on CALayers and CGContexts is much appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:21:53+00:00Added an answer on June 4, 2026 at 3:21 am

    I can suggest three different methods:

    1. Subclass CALayer and implement its drawInContext:.

    2. Set an object that is not a UIView as the sublayer’s delegate, then implement drawLayer:inContext:.

    3. Create a CGImage and set it as the layer’s contents:

      UIGraphicsBeginImageContext(size);
      CGContextRef context = UIGraphicsGetCurrentContext();
      
      /// draw
      
      UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      
      sublayer.contents = (id)image.CGImage;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to render some UIImages into 1 single image that I can save
I am trying to render video via the NDK, to add some features that
I'm trying to render some text using PIL, but the result that comes out
I'm trying to render some text and an image to a writeable bitmap to
Im trying to render emails on my site. The problem is that these emails
I'm trying to render to a texture, then draw that texture to the screen
I'm trying to render the sortable table that's provided in Google visualization API in
I'm trying to render some response like this def doAjax = Action { request
I'm trying to render out some images on an aspx page. Error I'm getting
I am trying to render a ViewModel using Razor and MVC3. For some reason

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.