2012-07-16 59 views
1
书面

我喜欢这样写的文字到图像:添加阴影到文本上UIIImage

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

如何将添加阴影这段文字?我知道我只需重新绘制黑色文字就可以移动几个像素,但有没有更简单的方法?

干杯, 乔治

回答

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