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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:36:51+00:00 2026-06-13T02:36:51+00:00

Background: I started my project in iOS 5 and built out a beautiful button

  • 0

Background: I started my project in iOS 5 and built out a beautiful button with layer. I added a textLayer onto the button and center it using the following code:

    float textLayerVerticlePadding = ((self.bounds.size.height - fontSize) /2);
    textLayer = [[CATextLayer alloc]init];
    [textLayer setFrame:CGRectOffset(self.bounds, 0, textLayerVerticlePadding)];

It works great and looks dead center until iOS 6.

Problem: iOS 6 added a space (padding) between the topmost bound and the text in textLayer. This upsets the calculation above. Is there a way to make sure that iOS 6 does not? because I would like to support both iOS 5 and 6 (for those who prefers Google Map).

Pictures:
This one is iOS 5 and the red color is the background of the textLayer (to make it more apparent)
enter image description here

And this one is iOS 6
enter image description here


Update: While im sure all the answers below are correct in their own ways, I found the post by t0rst simplest way to execute this. HelveticaNeue leaves a little space for both iOS5 and iOS6, unlike Helvetica which leaves no space on the top in iOS5 and little space in iOS6.

Update 2: Played around with it a little more, and found out the size of the little space. Without going into detail, the space is 1/6 of your font size. So to compensate for it I wrote

float textLayerVerticlePadding = ((self.bounds.size.height - fontSize) /2) - (fontSize/6);
[textLayer setFrame:CGRectOffset(self.bounds, 0, textLayerVerticlePadding)];

With that code, I get a dead center every time. Note that this is only tested with HelveticaNeue-Bold on iOS5 and iOS6. I cannot say for anything else.

  • 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-13T02:36:52+00:00Added an answer on June 13, 2026 at 2:36 am

    In iOS 5 and before, the first baseline in a CATextLayer is always positioned down from the top of the bounds by the ascent obtained from CTLineGetTypographicBounds when passed a CTLine made with the string for the first line.

    In iOS 6, this doesn’t hold true for all fonts anymore. Hence, when you are positioning a CATextLayer you can no longer reliably decide where to put it to get the right visual alignment. Or can you? …

    First, an aside: when trying to work out CATextLayer‘s positioning behaviour a while ago in iOS 5, I tried using all combinations of cap height, ascender from UIFont, etc. before finally discovering that ascent from CTLineGetTypographicBounds was the one I needed. In the process, I discovered that a) the ascent from UIFont ascender, CTFontGetAscent and CTLineGetTypographicBounds are inconsistent for certain typefaces, and b) the ascent is frequently strange – either cropping the accents or leaving way to much space above. The solution to a) is to know which value to use. There isn’t really a solution to b) other than to leave plenty of room above by offsetting CATextLayer bounds if it likely you will have accents that get clipped.

    Back to iOS 6. If you avoid the worst offending typefaces (as of 6.0, and probably subject to change), you can still do programatic positioning of CATextLayer with the rest of the typefaces. The offenders are: AcademyEngravedLetPlain, Courier, HoeflerText and Palatino – visually, these families position correctly (i.e. without clipping) in CATextLayer, but none of the three ascent sources gives you a usable indication of where the baseline is placed. Helvetica and .HelveticaNeueUI (aka system font) families position correctly with baseline at the ascent given by UIFont ascender, but the other ascent sources are not of use.

    Some examples from tests I did. The sample text is drawn three times in different colours. The coordinate origin is top left of grey box. Black text is drawn by CTLineDraw offset downwards by the ascent from CTLineGetTypographicBounds; transparent red is drawn by CATextLayer with bounds equal to the grey box; transparent blue is drawn with the UIKit NSString addition drawAtPoint:withFont: locating at the origin of the grey box and with the UIFont.

    1) A well behaved font, Copperplate-Light. The three samples are coincident, giving maroon, and meaning that the ascents are near enough the same from all sources. Same for iOS 5 and 6.

    copperplate-light iOS 6

    2) Courier under iOS 5. CATextLayer positions text too high (red), but CTLineDraw with ascent from CTLineGetTypographicBounds (black) matches CATextLayer positioning – so we can place and correct from there. NSString drawAtPoint:withFont: (blue) places the text without clipping. (Helvetica and .HelveticaNeueUI behave like this in iOS 6)

    courier iOS 5

    3) Courier under iOS 6. CATextLayer (red) now places the text so that it is not clipped, but the positioning no longer matches the ascent from CTLineGetTypographicBounds (black) or from UIFont ascender used in NSString drawAtPoint:withFont: (blue). This is unusable for programatic positioning. (AcademyEngravedLetPlain, HoeflerText and Palatino also behave like this in iOS 6)

    courier iOS 6

    Hope this helps avoid some of the hours of wasted time I went through, and if you want to dip in a bit deeper, have a play with this:

    - (NSString*)reportInconsistentFontAscents
    {
        NSMutableString*            results;
        NSMutableArray*             fontNameArray;
        CGFloat                     fontSize = 28;
        NSString*                   fn;
        NSString*                   sample = @"Éa3Çy";
        CFRange                     range;
        NSMutableAttributedString*  mas;
        UIFont*                     uifont;
        CTFontRef                   ctfont;
        CTLineRef                   ctline;
        CGFloat                     uif_ascent;
        CGFloat                     ctfont_ascent;
        CGFloat                     ctline_ascent;
    
        results = [NSMutableString stringWithCapacity: 10000];
        mas = [[NSMutableAttributedString alloc] initWithString: sample];
        range.location = 0, range.length = [sample length];
    
        fontNameArray = [NSMutableArray arrayWithCapacity: 250];
        for (fn in [UIFont familyNames])
            [fontNameArray addObjectsFromArray: [UIFont fontNamesForFamilyName: fn]];
        [fontNameArray sortUsingSelector: @selector(localizedCaseInsensitiveCompare:)];
        [fontNameArray addObject: [UIFont systemFontOfSize: fontSize].fontName];
        [fontNameArray addObject: [UIFont italicSystemFontOfSize: fontSize].fontName];
        [fontNameArray addObject: [UIFont boldSystemFontOfSize: fontSize].fontName];
    
        [results appendString: @"Font name\tUIFA\tCTFA\tCTLA"];
    
        for (fn in fontNameArray)
        {
            uifont = [UIFont fontWithName: fn size: fontSize];
            uif_ascent = uifont.ascender;
    
            ctfont = CTFontCreateWithName((CFStringRef)fn, fontSize, NULL);
            ctfont_ascent = CTFontGetAscent(ctfont);
    
            CFAttributedStringSetAttribute((CFMutableAttributedStringRef)mas, range, kCTFontAttributeName, ctfont);
            ctline = CTLineCreateWithAttributedString((CFAttributedStringRef)mas);
            ctline_ascent = 0;
            CTLineGetTypographicBounds(ctline, &ctline_ascent, 0, 0);
    
            [results appendFormat: @"\n%@\t%.3f\t%.3f\t%.3f", fn, uif_ascent, ctfont_ascent, ctline_ascent];
    
            if (fabsf(uif_ascent - ctfont_ascent) >= .5f // >.5 can round to pixel diffs in display
             || fabsf(uif_ascent - ctline_ascent) >= .5f)
                [results appendString: @"\t*****"];
    
            CFRelease(ctline);
            CFRelease(ctfont);
        }
    
        [mas release];
    
        return results;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: My REST service project was started up by using Hibernate. I use id
I would like to get started on a simple project using the Kohana 3
I have a project which downloads images in background using NSOperationQueue . It was
I recently started a project were I wanted the background image to scale to
Some background information: I am creating a Silverlight WCF RIA Services Project. I started
Just started coding in AS3 with FlashDevelop and coming from a C# background, I
Firstly some background,.. IVe started a new job as PHP developer with a company
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background - I am using paramiko to put files on a bunch of remote
Background I am working with a monad built of a stack of transformers one

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.