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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:11:39+00:00 2026-05-23T09:11:39+00:00

so I have a UILabel subclass that only crashes every so often on first

  • 0

so I have a UILabel subclass that only crashes every so often on first load. The label is in my root view controller so it is one of the first objects loaded. The problem is, it keeps crashing on a line in drawRect, which I will point out below.

I tried making sure the text is not nil and if it is, skipping out on the drawing because sizeWithFont: keeps giving me NaN errors, which lead to crashes at runtime.

If you are more experienced with CoreGraphics, please lend me a hand and tell me why this particular snippet is unstable:

- (void)drawRect:(CGRect)rect {
if(self.text == nil && self.text.length >! 0){
    return;
}
UIFont *font = self.font;
CGSize fontSize = [self.text sizeWithFont:font];
if(isnan(fontSize.width) == YES)return;
if(isnan(fontSize.height) == YES)return;
CGImageRef mask = [self createMaskWithSize:rect.size shape:^{
    [[UIColor blackColor] setFill];
    CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
    [[UIColor whiteColor] setFill];
    // custom shape goes here
    if(self.textAlignment == UITextAlignmentLeft){
        [self.text drawAtPoint:CGPointMake(0, 0) withFont:font];
        [self.text drawAtPoint:CGPointMake(0, -1) withFont:font];
    }else{
        [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), 0) withFont:font];
        [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];
    }
}];

CGImageRef cutoutRef = CGImageCreateWithMask([self blackSquareOfSize:rect.size].CGImage, mask);

UIImage *cutout = [UIImage imageWithCGImage:cutoutRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp]; 
    ^^ THIS IS WHAT CRASHES *******

CGImageRelease(cutoutRef);  

CGImageRef shadedMask = [self createMaskWithSize:rect.size shape:^{
    [[UIColor whiteColor] setFill];
    CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
    CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, .5), 2.5f, [[UIColor colorWithWhite:0.0 alpha:0.8] CGColor]);
    [cutout drawAtPoint:CGPointZero];
}];

// create negative image
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[[UIColor blackColor] setFill];
// custom shape goes here
if(self.textAlignment == UITextAlignmentLeft)[self.text drawAtPoint:CGPointMake(0, -1) withFont:font];
else [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -1) withFont:font];
UIImage *negative = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); 

CGImageRef innerShadowRef = CGImageCreateWithMask(negative.CGImage, shadedMask);
//CGImageRelease(shadedMask);
//UIImage *innerShadow = [UIImage imageWithCGImage:innerShadowRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];
CGImageRelease(innerShadowRef);

//Draw Bevel
[[UIColor whiteColor] setFill];
if(self.textAlignment == UITextAlignmentLeft)[self.text drawAtPoint:CGPointMake(0, 0.0) withFont:font];
else [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), 0.0) withFont:font];

// draw actual image
[self.textColor setFill];
if(self.textAlignment == UITextAlignmentLeft)[self.text drawAtPoint:CGPointMake(0, -0.5) withFont:font];
else [self.text drawAtPoint:CGPointMake((self.bounds.size.width/2)-(fontSize.width/2), -0.5) withFont:font];
// finally apply shadow
//[innerShadow drawAtPoint:CGPointZero];

}

Thanks!

  • 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-23T09:11:40+00:00Added an answer on May 23, 2026 at 9:11 am

    Your condition for testing self.text seems me wrong,

    Test with the below code …

     - (void)drawRect:(CGRect)rect {
        if(self.text == nil ){
            return;
        }
        else if([self.text length] == 0){
        return;
        }
        .............
        .............
        }//Close of drawRect:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that contains a UITableView and a UILabel which works perfectly
I have a subclass of a UILabel that overloads initWithCoder and I was wondering
I currently have a UIView subclass that acts as my header view for my
I have one UILabel and one button under the label. Height of the label
I have a UILabel that is a subview of a larger UIView. Is there
I have a UILabel in a custom UITableViewCell that gets resized when the device
I have this in my .h controller @interface helloViewController : UIViewController { IBOutlet UIlabel
I have a script that appends some rows to a table. One of the
I have a UILabel I created on one of my views in my application.
i have got two view. First: FirstViewController Second: SecondViewController FirstViewController is my UINavigationController 's

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.