- (UIImage *)imageWithString:(NSString *)string font:(UIFont *)font width:(CGFloat)width height:(CGFloat)height textAlignment:(NSTextAlignment)textAlignment
{
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor whiteColor] set];
CGRect rect = CGRectMake(0, 0, size.width , size.height);
CGContextFillRect(context, rect);
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = textAlignment;
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowBlurRadius = 0;//设置模糊度
shadow.shadowColor = [UIColor whiteColor];//设置阴影颜色
shadow.shadowOffset = CGSizeMake(1, 1);//设置阴影的偏移量
NSDictionary *attributes = @ {
NSForegroundColorAttributeName:[UIColor blackColor],
NSFontAttributeName:font,
NSShadowAttributeName:shadow,
NSParagraphStyleAttributeName:paragraph
};
[string drawInRect:rect withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}