开发者

UILabel subclass occasionally crashing on drawRect:

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.开发者_如何学Pythonwidth/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!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜