1

我正在将文本写入图像,如下所示:

    -(UIImage *)addLabelToImage:(UIImage *)img
{
    UIFont *font = [UIFont boldSystemFontOfSize:20];
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0,img.size.width,img.size.height)];
    CGRect rect = CGRectMake(60, 550, img.size.width, img.size.height);
    [[UIColor whiteColor] set];
    [[NSString stringWithFormat:@"%@, %@", [[NSUserDefaults standardUserDefaults] valueForKey:@"whatever"], [[NSUserDefaults standardUserDefaults] valueForKey:@"whatever2"]] drawInRect:CGRectIntegral(rect) withFont:font]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

我将如何在此文本中添加阴影?我知道我可以将文本重绘为移动几个像素的黑色文本,但有没有更简单的方法?

干杯,乔治

4

1 回答 1

3

在绘制文本之前,您可以像这样设置阴影:

//...
CGContextRef c = UIGraphicsGetCurrentContext();
CGSize offset = CGSizeMake(0, 1);
CGFloat blur = 2.0;
UIColor *shadowColor = [UIColor blackColor];
CGContextSetShadowWithColor(c, offset, blur, [shadowColor CGColor]);
//draw your text ...

根据需要调整offset,blurshadowColor参数以达到您想要的效果。对于锐利的阴影(如在 a 上UILabel),设置blur0

于 2012-07-16T18:14:18.087 回答