//文字转化成图片

-(UIImage *)imageFromText:(NSArray*)arrContent withFont:(CGFloat)fontSize withTextColor:(UIColor *)textColor withBgImage:(UIImage *)bgImage withBgColor:(UIColor *)bgColor

{

// set the font type and size

UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];

NSMutableArray *arrHeight = [[NSMutableArray alloc] initWithCapacity:arrContent.count];

CGFloat fHeight = 0.0f;

for (NSString *sContent in arrContent) {

CGSize stringSize = [sContent sizeWithFont:font constrainedToSize:CGSizeMake(CONTENT_MAX_WIDTH, 10000) lineBreakMode:NSLineBreakByWordWrapping];

[arrHeight addObject:[NSNumber numberWithFloat:stringSize.height]];

fHeight += stringSize.height;

}

CGSize newSize = CGSizeMake(CONTENT_MAX_WIDTH+20, fHeight+50);

// Create a stretchable image for the top of the background and draw it

UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);

CGContextRef ctx = UIGraphicsGetCurrentContext();

//如果设置了背景图片

if(bgImage)

{

UIImage* stretchedTopImage = [bgImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];

[stretchedTopImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

}else

{

if(bgColor)

{

//填充背景颜色

[bgColor set];

UIRectFill(CGRectMake(0, 0, newSize.width, newSize.height));

}

}

CGContextSetCharacterSpacing(ctx, 10);

CGContextSetTextDrawingMode (ctx, kCGTextFillClip);

[textColor set];

int nIndex = 0;

CGFloat fPosY = 20.0f;

for (NSString *sContent in arrContent) {

NSNumber *numHeight = [arrHeight objectAtIndex:nIndex];

CGRect rect = CGRectMake(10, fPosY, CONTENT_MAX_WIDTH , [numHeight floatValue]);

[sContent drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];

fPosY += [numHeight floatValue];

nIndex++;

}

// transfer image

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}

转载于:https://www.cnblogs.com/ios988/p/5630369.html

iOS 文字转化成图片相关推荐

  1. IOS-将长文字转化成图片方法

    我们在看微博时,会看到一些长图片上的显示文章,现在就介绍下如何实现.分析下还是很简单的,总结如下:1.计算文字区域的高 2.利用UIGraphics图形上下文方法来实现 3.验证方法:UIImageW ...

  2. python html转图片失真_html dom 转化成图片踩坑记(canvas toDataURL)

    需求 在开发过程中遇到这么一个需求,h5页面需要将一个html dom转化成图片,便于用户保存. 面向百度搜索第三方得 html2canvas 和 dom-to-image 两者在写这篇笔记之前在gi ...

  3. php网址图片怎么转based4,Ionic4 Base64 转化成图片插件-Base64 转化成图片Base64 To Gallery - Ionic Native...

    This plugin allows you to save base64 data as a png image into the device Ionic Base64 转化成图片Base64 T ...

  4. 想把文字转成图片?文字转图片制作软件哪个好用?

    现在社交媒体的使用越来越普及,为了让自己的内容更加吸引人,文字转成图片已经成为了一种流行的方式.那么,小伙伴在日常生活中是否有了解过文字转图片技术呢?接下来在这篇文章中,我将为大家介绍文字转图片制作软 ...

  5. python 脚本将视频转化成图片 | python scripts to convert video to pictures

    python 脚本将视频转化成图片 | python scripts to convert video to pictures python video_to_images.py 运行时,修改输入.输 ...

  6. 二、文字转成图片,图片上写字

    文字转成图片: from PIL import Image,ImageDraw,ImageFont def text2Image(self):text = u"这是一段测试文本,test 1 ...

  7. base64字符串转化成图片

    /*** base64字符串转化成图片** @param base64* @return*/ public static File base64ToFile(String base64) throws ...

  8. base64编码转换android,Android将base64编码转化成图片

    类似base64流的图片解析并展示: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBw ...

  9. 使用text2image将文字转换成图片

    text2image是一款将文字转换成图片的工具 官网:http://www.text2image.com/pit_t2i/saver 在做tesseract图片识别时需要基础数据采样 文档参考: h ...

最新文章

  1. Programmer of Practice Manual
  2. ThreadLocal的使用场景
  3. python写一个通讯录V2.0
  4. .Net 之匿名类(AnonymousType)(转载)
  5. python可以做什么项目-Python可以做大项目吗?
  6. 一个完整的python项目源码-Python实例练手项目汇总(附源码)
  7. 在Linux系统安装Nodejs 最简单步骤
  8. SpringBoot最佳实践-dev-tools
  9. RequireJS入门(一) 转
  10. 【计算机科学基础】ASCII码表知识总结
  11. HTML5 Canvas中绘制贝塞尔曲线
  12. ORA-12638 凭证检索失败
  13. echarts版本升级导致Chinamap渲染报错问题(vue2)
  14. PDF编辑器里的测量工具如何使用?
  15. soap xml 转 json
  16. java SE部分以及数据库学习笔记
  17. IP地址转换(C++)
  18. Netty网络编程实战2,使用Netty开发聊天室功能
  19. 快刀初试:Spark GraphX在淘宝的实践
  20. 如何截取阿里云oss的视频第一帧作为预览图片

热门文章

  1. NDK avi播放器
  2. android 百度移动搜索 url 参数,百度移动搜索开放适配服务的3种方法
  3. 查看Oracle数据库表空间大小(空闲、已使用),是否要增加表空间的数据文件
  4. python 数据分析工具之 numpy pandas matplotlib
  5. hibernate的二级缓存
  6. ThinkPHP 3.2 支付宝即时到账接口开发
  7. C语言中简单的for循环和浮点型变量
  8. SQL Server 2008 R2 中不能删除复制(replication) 的解决方法
  9. Centos7之Gcc安装
  10. [PHP]常量的一些特性