UILabel subclass occasionally crashing on drawRect?

Your condition for testing self. Text seems me wrong.

Up vote 0 down vote favorite share g+ share fb share tw.

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!

Iphone core-graphics uilabel drawrect link|improve this question asked Jun 19 '11 at 17:22clstroud936.

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.

I discovered this in parallel not long after I posted the question. I felt rather silly. Thank you!

– clstroud Jun 20 '11 at 3:36 4 Just to expand on the problems: 1. && is logical AND, meaning “if both of these things are true”. The condition reads as “If this pointer is nil AND the object's length is…”.

If the pointer is nil, you have no object, so asking it for its length is nonsense. 2. >!

Is not an operator in Objective-C. The compiler doesn't care about whitespace, so this actually is valid, but it's tokenized as two operators. A clearer statement of what it actually does is if length >!0.

!0 (logical-NOT zero) is 1, so this is comparing whether the length is greater than 1. – Peter Hosey Jun 20 '11 at 11:27 @Peter Hosey : I knew that but you explained it very well explanation – Jhaliya Jun 20 '11 at 12:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions