- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
imageView.backgroundColor = [UIColor cyanColor];
imageView.layer.cornerRadius = 50;
[self.view addSubview:imageView];
UILabel *temptext = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
temptext.text = @"你好帅";
temptext.textColor = [UIColor yellowColor];
temptext.textAlignment = 1;
UIImage *image = [self imageForView:temptext];
imageView.image = image;
}
//-(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;
}
iOS 将text文本转为image
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.在AppDelegate.m中添加下代码块: - (void)redirectNSlogToDocumentF...
- 从字符串中查关键字是否存在: 1)A3=find(A1,A2) //返回A1在A2中出现的初始位置 2)A3=if...
- 1.在AppDelegate.m中创建函数实现以下代码块: -(void)redirectNSlogToDocum...