将阴影添加到写在UIImage上的文本

时间:2012-07-16 18:10:17

标签: iphone objective-c sdk uiimage

我正在将文字写入图像:

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

如何在此文本中添加投影?我知道我可以只用几个像素重新绘制黑色文本,但有更简单的方法吗?

干杯, 乔治

1 个答案:

答案 0 :(得分: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 ...

根据需要调整offsetblurshadowColor参数,以达到您想要的效果。要获得清晰的阴影(例如UILabel),请将blur设置为0