2011-06-19 103 views
0

所以我有一個UILabel子類,它在第一次加載時只經常崩潰。該標籤位於我的根視圖控制器中,因此它是加載的第一個對象之一。問題是,它在drawRect的一行中不斷崩潰,我將在下面指出。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]; 

}

謝謝!

回答

2

您的測試self.text條件看來我錯了,

測試與下面的代碼...

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

我並行發現這個沒多久,我張貼的問題。我感覺很傻。謝謝! – clstroud

+4

只是爲了擴大問題:1. &&'邏輯與,意思是「如果這兩個事情都是真的」。條件如下:「如果這個指針是'nil'且對象的長度是...」。如果指針是'nil',你沒有任何對象,所以要求它的長度是無稽之談。 2.'>!'不是Objective-C中的運算符。編譯器不關心空白,所以這實際上是有效的,但它被標記爲兩個操作符。如果[length]>!0'更清楚地表明它實際做了什麼。 '!0'(logical-NOT zero)是1,所以這是比較長度是否大於1. –

+0

@彼得Hosey:我知道,但你解釋得很好解釋 – Jhaliya