一般情况下我们都习惯用webview加载富文本。但是webview比较消耗内存。然后发现其实本身可以用uilabel加载,性能还不错就记录下~~

核心方法下面两个

-(NSMutableAttributedString *)setAttributedString:(NSString *)str
{
//如果有换行,把\n替换成<br/>
//如果有需要把换行加上
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
//设置HTML图片的宽度
str = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",[UIScreen mainScreen].bounds.size.width,str];
NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
//设置富文本字的大小
[htmlString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:]} range:NSMakeRange(, htmlString.length)];
//设置行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:];
[htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(, [htmlString length])];
return htmlString;
} //计算html字符串高度
-(CGFloat )getHTMLHeightByStr:(NSString *)str
{
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
str = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",[UIScreen mainScreen].bounds.size.width,str]; NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
[htmlString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:]} range:NSMakeRange(, htmlString.length)];
//设置行间距
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setLineSpacing:];
[htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(, [htmlString length])]; CGSize textSize = [htmlString boundingRectWithSize:(CGSize){kScreenWidth - , CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
return textSize.height ;
}

记录一下UILabel加载富文本 iOS的更多相关文章

  1. android html 图片处理类--加载富文本工具类

    在android开发中,一些资讯类页面,里面有html标签和图片,html 标签一般通过Html.fromHtml方法,即可以解决,但是如果html 有图片标签,那么,Html.fromHtml 好像 ...

  2. Android WebView 加载富文本内容

    WebView加载数据的方式有两种: 1. webView.loadUrl(data);//加载url 2. webView.loadDataWithBaseURL(null,data, " ...

  3. ASP.NET中页面加载时文本框(texbox控件)内有文字获得焦点时文字消失

    代码如下: <asp:TextBox ID="TextBox1" runat="server" Height="26px" MaxLe ...

  4. #iOS问题记录#动态Html加载本地CSS和JS文件

    所谓动态Html,指代码中组合生成的html字符串: 若需要加载本地CSS,图片,JS文件,则, 1,需要文件的全路径: 2,需要"file:///"标志: 例如: //获取文件全 ...

  5. js实用方法记录-js动态加载css、js脚本文件

    js实用方法记录-动态加载css/js 附送一个加载iframe,h5打开app代码 1. 动态加载js文件到head标签并执行回调 方法调用:dynamicLoadJs('http://www.yi ...

  6. UILabel和UIbutton(富文本)封装方法

    /** 方法说明:设置label的富文本属性 参数说明:contentStr富文本内容 textColor字体颜色 rangeSet设置字体颜色及大小的位置 */ - (UILabel *)backf ...

  7. 记录Js动态加载页面.append、html、appendChild、repend添加元素节点不生效以及解决办法

    今天再优化blog页面的时候添加了个关注按钮和图片,但是页面上这个按钮和图片时有时无,本来是搞后端的,被这个前端的小问题搞得抓耳挠腮的! 网上各种查询解决方案,把我解决问题的艰辛历程分享出来,希望大家 ...

  8. libgdx学习记录16——资源加载器AssetManager

    AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...

  9. UILabel加载HTML

    NSString *string1 = @"<font color = \"red\">什么情况</font><br/>"; ...

随机推荐

  1. c语言ARP应用

    对于windows环境,winsock不能用来发ARP请求: 发表于: 2002-04-23 11:45:12 arp是请求硬件地址的.winsock层次太高啦... 用winsock中的sendto ...

  2. 启动Nginx 出现 nginx: [emerg] unknown directive "锘?user" 错误

    出现这种情况 一般是修改配置文件 nginx.conf 造成的 如果你修改文件后出现 那基本上就是这个原因 启动不了 重新打开 改为UTF-8 无BOM编码

  3. js函数(4)闭包

    8.6闭包 背景:3.10 变量作用域 在函数体内,局部变量的优先级高于同名的全局变量.如果在函数内声明一个局部变量或者函数参数中带有的变量和全局变量重名,则局部变量会覆盖全局变量: 在全局作用域编写 ...

  4. 【AtCoder】AGC008

    AGC008 A - Simple Calculator 如果符号相同,那么如果y比x大直接走,否则需要两次反号 如果符号不同,需要绝对值的差加一次反号 如果有一个是0,且y比x要小,那只需要一次反号 ...

  5. hdu 1024 最大m段不相交线段和

    题目传送门//res tp hdu 数据范围1e6,若是开二维会爆 考虑用滚动数组优化 #include<iostream> #include<cstdio> #include ...

  6. Ubuntu 下开发ARM

    1. 准备工作 linux下自带虚拟串口的驱动,不需要手动安装.CP2102之类的USB转串口,是ttyUSBx. 所有的设备都在/dev目录下,简单扫描串口的办法: ls /dev > bef ...

  7. python 定时爬取内容并发送报告到指定邮箱

    import requests import smtplib import schedule import time from bs4 import BeautifulSoup from email. ...

  8. 【opencv 源码剖析】 三、 morphOp 数学形态学滤波函数, 腐蚀和膨胀就是通过这个函数得到的

    // //_kernel : 形态学滤波的核 //anchor: 锚点再滤波核的位置 //iterations: 迭代次数 static void morphOp( int op, InputArra ...

  9. ActiveMQ入门系列二:入门代码实例(点对点模式)

    在上一篇<ActiveMQ入门系列一:认识并安装ActiveMQ(Windows下)>中,大致介绍了ActiveMQ和一些概念,并下载.安装.启动他,还访问了他的控制台页面. 这篇,就用代 ...

  10. LLVM新建全局变量

    在LLVM中,有原生的AST Clone,却没有一个比较好的Stmt copy功能,基于Scout在LLVM上进行的修改,我们实现了自己的Stmt Clone功能. 要进行Stmt Clone,肯定需 ...