2012-06-15 2 views
0

J'utilise -drawRect pour la première fois dans un UITableViewCell personnalisé, et je veux savoir comment je pourrais ajouter 1px dropShadows à tout le texte étant dessiné, et l'image (self.image).DrawRect ajouter du texte ombre

Merci d'avance.

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

Répondre

2

Utilisation:

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

Le premier paramètre est le contexte, le deuxième paramètre est le paramètre de décalage, le troisième est flou.

Quartz2d Docs on Shadows

+0

Parfait, merci. –

+0

Une note de côté, faites attention à 'Les conventions de dessin d'ombre varient en fonction du contexte 'car vous devrez peut-être retourner votre coordonnée Y sur le décalage. – CrimsonDiego