UILabel 子类偶尔会在绘制矩形上崩溃:

发布于 2024-11-16 04:52:32 字数 3131 浏览 4 评论 0原文

所以我有一个 UILabel 子类,它只会在第一次加载时经常崩溃。该标签位于我的根视图控制器中,因此它是第一个加载的对象之一。问题是,它一直在drawRect中的一行上崩溃,我将在下面指出。

我尝试确保文本不为零,如果是,则跳过绘图,因为 sizeWithFont: 不断给我 NaN 错误,这会导致运行时崩溃。

如果您对 CoreGraphics 更有经验,请帮助我并告诉我为什么这个特定的代码片段不稳定:

- (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];

}

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2024-11-23 04:52:32

你的测试 self.text 的条件似乎是错误的,

用下面的代码测试......

 - (void)drawRect:(CGRect)rect {
    if(self.text == nil ){
        return;
    }
    else if([self.text length] == 0){
    return;
    }
    .............
    .............
    }//Close of drawRect:

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:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文