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

  • Home
  • SEARCH
  • 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 8407307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:19:28+00:00 2026-06-09T23:19:28+00:00

I did the following : – (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { UIGraphicsPushContext ( ctx );

  • 0

I did the following :

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{

      UIGraphicsPushContext ( ctx );

      CGRect r = CGRectMake( 500, 300, 200, 100 );
      NSString *text = [[NSString alloc] initWithString:@"raaaaaaaa!"];
      UIColor *color = [ UIColor colorWithRed: (200.0f)  green: (100.0)  blue:(200.0f) alpha: 1.0f ];
      [color set];

      [ text drawInRect: r withFont:[UIFont systemFontOfSize:50] lineBreakMode: UILineBreakModeWordWrap alignment: UITextAlignmentLeft ];
      [text release];

      UIGraphicsPopContext();
    }
  }
}

The above code is not working. Why?

If i do the following instead, it is working:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
      UIGraphicsPushContext ( ctx );

      CATextLayer *label = [[CATextLayer alloc] init];
      [label setFont:@"Didot"];
      [label setFontSize:50];  
      [label setFrame:CGRectMake( 400, 300, 400, 100 )];
      [label setString:@"Helloooooooooooooooooo"];
      [label setAlignmentMode:kCAAlignmentCenter];
      [label setForegroundColor:[[UIColor blueColor] CGColor]];
      [layer addSublayer:label];
      [label release];

      UIGraphicsPopContext();
  }
}

What is the difference and why drawInRect does nothing?

Thank you

EDIT
So, I was not using UIColor properly, so I changed

UIColor *color = [ UIColor colorWithRed: (200.0f)  green: (100.0)  blue:(200.0f) alpha: 1.0f ];

for this

UIColor *color = [UIColor colorWithRed:200 / 255.0 green:100 / 255.0 blue: 200 / 255.0 alpha: 1.0];

Still no luck, I can’t see any text when using drawInRect

  • 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-09T23:19:30+00:00Added an answer on June 9, 2026 at 11:19 pm

    There are two questions in you question:

    The above code is not working. Why?

    At first I thought that the text didn’t show up because it was drawn with white on white. While it may still have been so, that is not the only problem.

    The rectangle that you are drawing your text into (r) is relative to the bounds of the layer you are drawing in. I.e. if you want to draw in the top left corner of the layer, the origin of the rect should be (0, 0). The above code is still not working though.

    If you inspect the CGSize that drawInRect:withFont:… returns (the size of the rendered text) you can see that the size of the rendered text is 0 wide. This is an indication that the height of 100 for the rectangle to draw in is not enough. Increasing the height of the rectangle solves this and now the text is visible. If you are drawing some text inside a layer, maybe the most sensible rectangle to draw in is the bounds of the layer (CGRect r = [layer bounds];).

    What I did to make it work:

    • Changed text color
    • Changed origin of r to (0,0)
    • Increased height of r

    What is the difference […] ?

    In the first you are drawing text into the graphics context of the layer (what drawLayer:inContext: is supposed to do) but in the seconds example you are adding another layer to the hierarchy of the layer instead of drawing inside it. Every time your layer will redraw, a new text layer will be added to it. So nothing is actually drawn into the layers graphics context.

    What happens next, after your layer has drawn itself (empty), is that the sublayers of your layers draw themselves and their content is composed on top of your layer. That is why you see the text of the text layer.

    If you add a text layer like this you can make your code more efficient by not having to do any manual drawing. This requires that the text layer is added to your layer at some earlier point (only once) and that you don’t do any custom drawing (it’s often slow).

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

Sidebar

Related Questions

I did the following steps to use the CDialog in win 32 application: Changed
I did the following but it not work for always.It works if i launch
Im a clojure beginner.I did the following steps as given on http://www.unexpected-vortices.com/clojure/brief-beginners-guide/development-env.html to setup
hay all, I just did the following: a = input(give a word: ) b
How can I create a const boost matrix? The following did not work: const
Did you ever have the following situation: you need to store information, but a
I did my research but haven't found an answer to the following problem: I
Consider the following code (for simplicity, I did not follow any C# coding rules).
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
f'I have the following: A---B---C-----D-- branch dev \--C'-E-/ branch test I did it bad:

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.