0

I am using -drawRect for the first time in a custom UITableViewCell, and I want to know how I could add 1px dropShadows to all of the text being drawn, and the image (self.image).

Thanks in advance.

- (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]];
}
Bruno Koga
  • 3,864
  • 2
  • 34
  • 45
max_
  • 24,076
  • 39
  • 122
  • 211

1 Answers1

2

Use:

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

The first parameter is context, second parameter is offset, third parameter is blur.

Quartz2d Docs on Shadows

CrimsonDiego
  • 3,616
  • 1
  • 23
  • 26
  • A side note, pay attention to `Shadow Drawing Conventions Vary Based on the Context` as you may need to flip your Y coordinate on the offset. – CrimsonDiego Jun 15 '12 at 21:11