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

The Archive Base Latest Questions

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

I’m trying to create a custom label subclass that draws rich text and in

  • 0

I’m trying to create a custom label subclass that draws rich text and in which I can set different parameters. One of the most huge request I have is about line breaking. In UILabel is fixed and sometimes it doesn’t fit the graphics requirements.
Thus, helping myself with a little snippet on Apple site, I’ve started writing my own classes, and it works (somehow), but I’m having one problems:

  1. If the line is only one, the text is not aligned vertically, it always starts a the top left of the screen

Here is the code I’ve written so far:

- (void)drawRect:(CGRect)rect
{
    if (_text) {

        if (!_attribstring) {
            [self createAttributedString];
        }
        if (self.lineBreakValue == 0) {
            self.lineBreakValue = 9;
        }

        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(ctx, 0, self.bounds.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0); 
        CGContextSetShouldSmoothFonts(ctx, YES);
        CGContextSetShouldAntialias(ctx, YES);
        CGRect textRect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);

        //Manual Line braking
        BOOL shouldDrawAnotherLine = YES;
        double width = textRect.size.width;
        CGPoint textPosition = CGPointMake(textRect.origin.x, textRect.origin.y+textRect.size.height-self.lineBreakValue);
        ;
        // Initialize those variables.

        // Create a typesetter using the attributed string.
        CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef )  self.attribstring);

        // Find a break for line from the beginning of the string to the given width.
        CFIndex start = 0;
        while (shouldDrawAnotherLine) {

            CFIndex count = CTTypesetterSuggestLineBreak(typesetter, start, width);

            // Use the returned character count (to the break) to create the line.
            CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, count));

            // Get the offset needed to center the line.
            float flush = 0.5; // centered
            double penOffset = CTLineGetPenOffsetForFlush(line, flush, width);

            // Move the given text drawing position by the calculated offset and draw the line.
            CGContextSetTextPosition(ctx, textPosition.x + penOffset, textPosition.y);
            CTLineDraw(line, ctx);
            if (line!=NULL) {
                CFRelease(line);
            }
            // Move the index beyond the line break.

            if (start + count >= [self.attribstring.string length]) {
                shouldDrawAnotherLine = NO;
                continue;
            }
            start += count;
            textPosition.y-=self.lineBreakValue;
        }
        if (typesetter!=NULL) {
            CFRelease(typesetter);

        }

    }

}

Can someone point me to the right direction?
Regards,
Andrea

  • 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:06:56+00:00Added an answer on June 4, 2026 at 3:06 am

    For laying out text vertically aligned, you need to know the total height of the lines, and the y position of the first line would be:

    (self.bounds.size.height - (totalLineHeight)) / 2 - font.ascent;
    

    So you need 2 loops in your code, one for calculating the total height of the lines(you can also save char count of each line for later use in another loop for drawing), another certainly for drawing the lines starting from the y position calculated using the above formula.

    Note: Height of each line can be the font size, you can also add line spacing between lines, all you need to ensure is that being consistent in calculating the line height and drawing those lines in regards of the y position.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.