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 7906019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:44:07+00:00 2026-06-03T10:44:07+00:00

I am having a problem of drawing a character in a CATextLayer such that

  • 0

I am having a problem of drawing a character in a CATextLayer such that the layer is exactly the size of the character.

I use the code below to get the size of the glyph corresponding to the character in the string. At the moment I am neglecting diacretics, so that I am assuming that there is a one to one correlation between glyphs and characters.

I also get nice bounding box values for several characters for the Helvetica font at size 128pt:

Character | x   | y     | width | height |
B         | 9.4 |   0.0 | 70.8  | 91.8   |
y         | 1.3 | -27.4 | 61.2  | 96.0   |

I am not sure where the origin of the coordinate system is located in which the coordinates are expressed. I am assuming that (0,0) is at the very left and vertically located on the baseline of the font. That is why ‘y’ has a negative y value.

I am using this code to calculate the size of a capital B and resize its CATextLayer accordingly.

- (CATextLayer *) testTextLayer
{
    CATextLayer *l = [CATextLayer layer];
    l.string = @"B";
    NSUInteger len = [l.string length];
    l.fontSize =128.f;
    CGColorRef blackColor = CGColorCreateGenericGray(0.f, 1.f);
    l.foregroundColor = blackColor;
    CGColorRelease(blackColor);

    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.string, CFRangeMake(0, [l.string length]), characters);

    // Get CTFontRef from CGFontRef
    CTFontRef coreTextFont = CTFontCreateWithGraphicsFont(layerFont, l.fontSize, NULL, NULL);

    // allocate glyphs and bounding box arrays for holding the result
    // assuming that each character is only one glyph, which is wrong
    CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph)*len);
    CTFontGetGlyphsForCharacters(coreTextFont, characters, glyphs, len);

    // get bounding boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CTFontGetBoundingRectsForGlyphs(coreTextFont, kCTFontDefaultOrientation, glyphs, bb, len);
    CFRelease(coreTextFont);

    l.position = CGPointMake(200.f, 100.f);

    l.bounds = bb[0];

    l.backgroundColor = CGColorCreateGenericRGB(0.f, .5f, .9f, 1.f);

    free(characters);
    free(glyphs);
    free(bb);

    return l;
}

This is the result I am getting from the above code. It seems to me that the size is correct, however there is some kind of padding taking place around the character.

Result of the B on a CATextLayer

Now my questions

  1. Am I right with the assumption of the origin of the bounding box of the glyph?
  2. How can one draw the letter such that it fits neatly into the layer, without this padding? Or alternatively, how can one control this padding?

Maybe I am missing an obvious point here. Is there now way after setting the size and the font of the layer to shrink wrap the layer around the character in a defined way (meaning with optional padding, a bit like in CSS)?


  • 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-03T10:44:08+00:00Added an answer on June 3, 2026 at 10:44 am

    How about creating a CGPath from a glyph with CTFontCreatePathForGlyph and then getting its bounding box with CGPathGetBoundingBox?

    An alternative would be to create a CTRun somehow and use the CTRunGetImageBounds function which also returns a “tight” bounding box, but this probably requires more lines of code than the aforementioned approach and you’d need to have a graphics context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a graphics problem on drawing lines in Flash Player, where two lines
I'm having a strange problem that I can't work out and wonder if anybody
I'm having trouble with a gradient drawing call. I have a Form that looks
I'm having problems with some AS3 code that simply draws on a Sprite 's
I'm having a problem with custom cursors in a WPF application. I use the
Is it possible to use the FromStream method of System.Drawing.Image without having to keep
I have some code that does custom drawing. Basically it is form fill program
I'm having a strange problem - I have about 14.5 million bitmap images, that
I'm custom drawing a menu item in a MenuStrip . The problem I'm having
Whilst drawing on a SurfaceView, I'm having a problem intercepting a 'back' key press.

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.