直接上代码
- (UIImage *)imageFromString:(NSString *)string attributes:(NSDictionary *)attributes size:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[string drawInRect:CGRectMake(0, 0, size.width, size.height) withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)imageForView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
else
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 40, 40)];
imgview.backgroundColor = [UIColor colorWithRed:160/255.0 green:176/255.0 blue:213/255.0 alpha:1.0];
imgview.layer.cornerRadius = 20;
[self.view addSubview:imgview];
UILabel *temptext = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
temptext.text = @"好";
temptext.textColor = [UIColor whiteColor];
temptext.textAlignment = NSTextAlignmentCenter;
UIImage *image = [self imageForView:temptext];
imgview.image = image;
/*
NSString *string = @"字";
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
[paragraphStyle setLineSpacing:15.f]; //行间距
[paragraphStyle setParagraphSpacing:2.f];//字符间距
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:12],
NSForegroundColorAttributeName : [UIColor blueColor],
NSBackgroundColorAttributeName : [UIColor clearColor],
NSParagraphStyleAttributeName : paragraphStyle, };
UIImage *image1 = [self imageFromString:string attributes:attributes size:imgview.bounds.size];
imgview.image = image1;
*/
}
效果: