【问题标题】:UILabel subclass occasionally crashing on drawRect:UILabel 子类偶尔会在 drawRect 上崩溃:
【发布时间】:2011-06-19 17:22:57
【问题描述】:

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

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

如果您对 CoreGraphics 有更多的经验,请帮帮我,告诉我为什么这个特定的 sn-p 不稳定:

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

}

谢谢!

【问题讨论】:

    标签: iphone core-graphics uilabel drawrect


    【解决方案1】:

    你测试self.text的条件好像我错了,

    用下面的代码测试...

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

    【讨论】:

    • 我在发布问题后不久发现了这一点。我觉得很傻。谢谢!
    • 只是为了扩展问题: 1. && 是逻辑与,意思是“如果这两个都是真的”。条件读取为“如果此指针是nil 并且对象的长度是……”。如果指针是nil,你没有对象,所以问它的长度是无稽之谈。 2. >! 不是 Objective-C 中的运算符。编译器不关心空格,所以这实际上是有效的,但它被标记为两个运算符。 if [length] > !0 更清楚地说明了它的实际作用。 !0(逻辑非零)为1,所以这是比较长度是否大于1。
    • @Peter Hosey:我知道但你解释得很好解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多