IOS-将长文字转化成图片方法
我们在看微博时,会看到一些长图片上的显示文章,现在就介绍下如何实现。分析下还是很简单的,总结如下:1、计算文字区域的高 2、利用UIGraphics图形上下文方法来实现 3、验证方法:UIImageWriteToSavedPhotosAlbum,在本地相册中查看成功与否。
-(UIImage *)imageFromText:(NSArray*) arrContent withFont: (CGFloat)fontSize
{
// set the font type and size
UIFont *font = [UIFontsystemFontOfSize:fontSize];
NSMutableArray *arrHeight = [[NSMutableArrayalloc] initWithCapacity:arrContent.count];
CGFloat fHeight = 0.0f;
for (NSString *sContent in arrContent) {
CGSize stringSize = [sContent sizeWithFont:font constrainedToSize:CGSizeMake(CONTENT_MAX_WIDTH, 10000) lineBreakMode:UILineBreakModeWordWrap];
[arrHeight addObject:[NSNumbernumberWithFloat:stringSize.height]];
fHeight += stringSize.height;
}
CGSize newSize = CGSizeMake(CONTENT_MAX_WIDTH+20, fHeight+50);
UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetCharacterSpacing(ctx, 10);
CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);
CGContextSetRGBFillColor (ctx, 0.1, 0.2, 0.3, 1); // 6
CGContextSetRGBStrokeColor (ctx, 0, 0, 0, 1);
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:UILineBreakModeWordWrapalignment:UITextAlignmentLeft];
fPosY += [numHeight floatValue];
nIndex++;
}
// transfer image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
return image;
}
IOS-将长文字转化成图片方法的更多相关文章
- iOS中 将 颜色转化成图片
定义一个类方法: 声明: + (UIImage *)imageFromColor:(UIColor *)color; 实现: + (UIImage *)imageFromColor:(UIColor ...
- html dom 转化成图片踩坑记(canvas toDataURL)
需求 在开发过程中遇到这么一个需求,h5页面需要将一个html dom转化成图片,便于用户保存. 面向百度搜索第三方得 html2canvas 和 dom-to-image 两者在写这篇笔记之前在gi ...
- [js常用]文字转化成语音
使用百度语音接口,实现文字转化成语音播放 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &qu ...
- 在linux环境下使用icepdf或pdfbox将pdf转化成图片是乱码解决
在linux环境下使用icepdf或pdfbox将pdf转化成图片是出现乱码,网上查发下是itextpdf生成pdf引用"STSong-Light"字体而linux环境下没有这个字 ...
- base64字符串转化成图片
package com.dhht.wechat.util; import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; import ja ...
- 用JavaScript将Canvas内容转化成图片的方法
上周我们花了半天时间开发下一个准备放进Mozilla Marketplace的应用.有一个应用现在非常的火热,那就是Instagram,Facebook花了100万美元收购了它.我们也想有100万美元 ...
- PHP实现新浪长链接转化成短链接API
我们经常收到类似于这样的短信(如下图),发现其中的链接并不是常规的网址链接,而是个短小精悍的短链接,产品中经常需要这样的需求,如果在给用户下发的短信中是一个很长的连接,用户体验肯定很差,因此我们需要实 ...
- 使用的是html5的canvas将文字转换成图片
当前功能的运用场景是:用户需要传文件给他人,在用户选择文件之后需要显示一个文件图标和所选文件的名称. 当前代码部分是摘自网上,但是已经忘记在什么地方获取的,如有侵权联系小弟后自当删除. 注意:必须在h ...
- byte[] 转成图片方法
/// <summary> /// ImageData 的摘要说明 /// </summary> public class ImageData : IHttpHandler { ...
随机推荐
- C# 数据库dataGridView刷新数据和主外键判断
本文主要讲诉在使用VS2012+SQL Server数据库做系统中,通常会遇到几个问题.使用dataGridView控件在修改.删除.插入数据后,怎样刷新数据显示操作后的结果.同时在对数据操作时通常会 ...
- MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions -(亲测可用)
MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions - See more at: http://www.co ...
- postgreSQL远程连接出现:Error connecting to server :致命错误 SSL关闭的pg_hba.conf记录
异常截图:
- leetcode Count and Say python
class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str " ...
- phpcms-v9 前台模板文件中{pc}标签的执行流程
前台pc标签的使用:{pc:content 参数名="参数值" 参数名="参数值" 参数名="参数值"} 如: {pc:content ac ...
- js解决click事件点击事件间隔方法
var myTimeout = null; $("#id").click(function(){ clearTimeout(myTimeout); myTimeout = setT ...
- HTML前端技术(JS的使用,包括数组和字符串)
<script type="text/javascript"> /* JS 数组的操作 //concat 连接两个或更多的数组,并返回结果. var arr1 = ne ...
- DB2 相关操作
查看数据库版本:db2level DB21061E Command line environment not initialized. windows命令行下,需要先调用db2cmd,或者DB2 ...
- Traveling
Problem J: Traveling Time Limit: 1 Sec Memory Limit: 32 MB Description SH likes traveling around th ...
- 复习-C语言内嵌汇编-初级(2)
汇编取出内存中的值 # include <stdio.h> int main() { ; ; int *p = &i; //ret = *p; __asm__( "ldr ...