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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:52:20+00:00 2026-06-10T21:52:20+00:00

I draw some text to UIVIew in drawRect: . First I calculate text height

  • 0

I draw some text to UIVIew in drawRect:. First I calculate text height and then drawInRect:. The code bellow works:

- (void)drawRect:(CGRect)rect
{
    CGFloat titleHeight = [self heightForText:_entry.title
                                     withFont:[UIFont systemFontOfSize:12.0f]];

    CGRect r = CGRectMake(54, 6, kCellTextWidth, titleHeight);
    [_entry.title drawInRect:r withFont:[UIFont titleFont]];
}

Then I calculate text height with dispatch_async and drawInRect in main_queue, it fails:

- (void)drawRect:(CGRect)rect
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        CGFloat titleHeight = [self heightForText:_entry.title
                                         withFont:[UIFont systemFontOfSize:12.0f]];

        dispatch_async(dispatch_get_main_queue(), ^{
            CGRect r = CGRectMake(54, 6, kCellTextWidth, titleHeight);
            [_entry.title drawInRect:r withFont:[UIFont titleFont]];
        });
    });
}

error:

 <Error>: CGContextSetFont: invalid context 0x0
 <Error>: CGContextSetTextMatrix: invalid context 0x0
 <Error>: CGContextSetFontSize: invalid context 0x0
 <Error>: CGContextSetTextPosition: invalid context 0x0
 <Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0

What’s the error mean? How can I calculate text height in another thread in order to improve speed?

Thank you.

  • 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-10T21:52:22+00:00Added an answer on June 10, 2026 at 9:52 pm

    You can use CoreGraphics and CoreText APIs from secondary threads.

    There are only very few UIKit APIs which are safe to call from secondary threads. As a general rule, UIKit APIs are intended for the main thread only.

    The error means that NULL contexts are being passed to CG-APIs. The secondary threads do not create one by default — you must create your own rendering context and destination to render on a secondary thread. UIKit calls just grab the top context on the thread’s context stack — the context stack does not exist on the secondary thread, or in this context.

    As far as speed — it’s hard to believe one label would cause such a noticeable slow down. Perhaps more context would help.

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

Sidebar

Related Questions

I'm trying to simply draw some text with a stroke and then apply a
I have a custom UIView class, and in the - (void)drawRect:(CGRect)rect I want to
I have some code in .Net to draw some text content using GDI+. I
I am using the DrawRect method to draw some text and can't figure out
I am trying to draw some text with CoreGraphics. Unfortunately there is no method
I'm using Silverlight to draw some shapes and text on top of an image.
I'd like to draw text on some location on the screen without using UILabel
I am trying to draw some text in my CPView, I have this: -
I want to draw some text in a view, rotated 90°. I'm pretty new
I want to use the canvas.drawText() method to draw some text frame after frame.

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.