2012-06-15 157 views
0

我在自定義UITableViewCell中首次使用了-drawRect,我想知道如何將1px的dropShadows添加到所有正在繪製的文本以及圖像(self.image)。DrawRect添加文字陰影

在此先感謝。

- (void) drawRect:(CGRect) rect { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [[UIColor clearColor] set]; 
    CGContextFillRect(context, rect); 

    if (shouldDrawImage == YES) { 
     CGContextDrawImage(context, CGRectMake(10, 10, 40, 40), self.image.CGImage); 
    } 

    CGContextDrawImage(context, CGRectMake(self.frame.size.width - 16, 0, 16, self.frame.size.height), [UIImage imageNamed:@"right_bar_including_holes"].CGImage);  
    NSString *authorName = [[self.info objectForKey:@"user"] objectForKey:@"full_name"]; 

    [RGB(219, 240, 73) set]; 

    CGSize maximumLabelSize = CGSizeMake(self.frame.size.width - 10 - 55 - 16,9999); 
    CGSize authorsize = [authorName sizeWithFont:[UIFont boldSystemFontOfSize:15] 
            constrainedToSize:maximumLabelSize 
             lineBreakMode:UILineBreakModeWordWrap]; 
    [authorName drawInRect:CGRectMake(60, 10, self.frame.size.width - 60, authorsize.height) withFont:[UIFont boldSystemFontOfSize:15]]; 

    [RGB(249,249,249) set]; 

    NSString *description = [self.info objectForKey:@"description"]; 
    CGSize descriptionSize = [description sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; 

    [description drawInRect:CGRectMake(60, authorsize.height + 15, descriptionSize.width, descriptionSize.height) withFont:[UIFont systemFontOfSize:14]]; 
} 

回答

2

使用:

CGContextSaveGState(context); 
CGContextSetShadow(context, CGSizeMake(1,1),1); 
//draw text here 
CGContextRestoreGState(context); 

第一個參數是上下文中,第二個參數是偏移量,第三個參數是模糊。

Quartz2d Docs on Shadows

+0

完美,謝謝。 –

+0

一個側面提示,注意'陰影繪製約定基於上下文',因爲您可能需要在偏移量上翻轉Y座標。 – CrimsonDiego