-(UIImage *)imageFromText:(NSString *)text width:(float)width height:(float)height
{
// set the font type and size
UIFont *font = [UIFont systemFontOfSize:12.0];
CGSize size = CGSizeMake(width, height);// [text sizeWithFont:font];
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);
// optional: add a shadow, to avoid clipping the shadow you should make the context size bigger
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]);
// draw in context, you can use drawInRect/drawAtPoint:withFont:
//[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
[text drawInRect:CGRectMake(0, 0, width, height) withFont:font];
// transfer image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
[image retain];
UIGraphicsEndImageContext();
return image;
}
iPhone:NSString生成UIImage
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:NSValue
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
iPhone:UIImage 图像截取,等比例缩放
代码实现iPhone中UIImage的图像截取,等比例缩放
移动开发 iphone 缩放 截取 UIImage