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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:00:18+00:00 2026-05-27T17:00:18+00:00

All my research so far seems to indicate it is not possible to do

  • 0

All my research so far seems to indicate it is not possible to do this accurately. The only two options available to me at the outset were:

a) Using a Layout manager for the CATextLayer – not available on iOS as of 4.0

b) Use sizeWithFont:constrainedToSize:lineBreakMode: and adjust the frame of the CATextLayer according to the size returned here.

Option (b), being the simplest approach, should work. After all, it works perfectly with UILabels. But when I applied the same frame calculation to CATextLayer, the frame was always turning out to be a bit bigger than expected or needed.

As it turns out, the line-spacing in CATextLayers and UILabels (for the same font and size) is different. As a result, sizeWithFont (whose line-spacing calculations would match with that of UILabels) does not return the expected size for CATextLayers.

This is further proven by printing the same text using a UILabel, as against a CATextLayer and comparing the results. The text in the first line overlaps perfectly (it being the same font), but the line-spacing in CATextLayer is just a little shorter than in UILabel. (Sorry I can’t upload a screenshot right now as the ones I already have contain confidential data, and I presently don’t have the time to make a sample project to get clean screenshots. I’ll upload them later for posterity, when I have the time)

This is a weird difference, but I thought it would be possible to adjust the spacing in the CATextLayer by specifying the appropriate attribute for the NSAttributedString I use there, but that does not seem to be the case. Looking into CFStringAttributes.h I can’t find a single attribute that could be related to line-spacing.

Bottomline:

So it seems like it’s not possible to use CATextLayer on iOS in a scenario where the layer is required to fit to its text. Am I right on this or am I missing something?

P.S:

  1. The reason I wanted to use CATextLayer and NSAttributedString’s is because the string to be displayed is to be colored differently at different points. I guess I’d just have to go back to drawing the strings by hand as always….of course there’s always the option of hacking the results from sizeWithFont to get the proper line-height.

  2. Abusing the ‘code’ tags a little to make the post more readable.

  3. I’m not able to tag the post with ‘CATextLayer’ – surprisingly no such tags exist at the moment. If someone with enough reputation bumps into this post, please tag it accordingly.

  • 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-05-27T17:00:19+00:00Added an answer on May 27, 2026 at 5:00 pm

    Try this:

    - (CGFloat)boundingHeightForWidth:(CGFloat)inWidth withAttributedString:(NSAttributedString *)attributedString {
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( (CFMutableAttributedStringRef) attributedString); 
        CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(inWidth, CGFLOAT_MAX), NULL);
        CFRelease(framesetter);
        return suggestedSize.height;
    }
    

    You’ll have to convert your NSString to NSAttributedString. In-case of CATextLayer, you can use following CATextLayer subclass method:

    - (NSAttributedString *)attributedString {
    
        // If string is an attributed string
        if ([self.string isKindOfClass:[NSAttributedString class]]) {
            return self.string;
        }
    
        // Collect required parameters, and construct an attributed string
        NSString *string = self.string;
        CGColorRef color = self.foregroundColor;
        CTFontRef theFont = self.font;
        CTTextAlignment alignment;
    
        if ([self.alignmentMode isEqualToString:kCAAlignmentLeft]) {
            alignment = kCTLeftTextAlignment;
    
        } else if ([self.alignmentMode isEqualToString:kCAAlignmentRight]) {
            alignment = kCTRightTextAlignment;
    
        } else if ([self.alignmentMode isEqualToString:kCAAlignmentCenter]) {
            alignment = kCTCenterTextAlignment;
    
        } else if ([self.alignmentMode isEqualToString:kCAAlignmentJustified]) {
            alignment = kCTJustifiedTextAlignment;
    
        } else if ([self.alignmentMode isEqualToString:kCAAlignmentNatural]) {
            alignment = kCTNaturalTextAlignment;
        }
    
        // Process the information to get an attributed string
        CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    
        if (string != nil)
            CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef)string);
    
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTForegroundColorAttributeName, color);
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTFontAttributeName, theFont);
    
        CTParagraphStyleSetting settings[] = {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment};
        CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTParagraphStyleAttributeName, paragraphStyle);    
        CFRelease(paragraphStyle);
    
        NSMutableAttributedString *ret = (NSMutableAttributedString *)attrString;
    
        return [ret autorelease];
    }
    

    HTH.

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

Sidebar

Related Questions

All my research so far suggests this can't be done, but I'm hoping someone
Sorry this is a basic question, but all my research just barely missed answering
I made a research and all posts here are very blury regarding this issue.
This seems to be asked a lot on the internet, but so far my
All, I've been doing some research on when (and when not) to use Memcached.
I did some research but all I could find was syncing data core with
First of all, I've done my research and I did find a bunch of
I'm doing some research on the Groovy programming language, and despite all the information
All -- I have these two methods in one of my classes: public static
For my application I am using a couple listview. So far have been using

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.