记录一下UILabel加载富文本 iOS
一般情况下我们都习惯用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的更多相关文章
- android html 图片处理类--加载富文本工具类
在android开发中,一些资讯类页面,里面有html标签和图片,html 标签一般通过Html.fromHtml方法,即可以解决,但是如果html 有图片标签,那么,Html.fromHtml 好像 ...
- Android WebView 加载富文本内容
WebView加载数据的方式有两种: 1. webView.loadUrl(data);//加载url 2. webView.loadDataWithBaseURL(null,data, " ...
- ASP.NET中页面加载时文本框(texbox控件)内有文字获得焦点时文字消失
代码如下: <asp:TextBox ID="TextBox1" runat="server" Height="26px" MaxLe ...
- #iOS问题记录#动态Html加载本地CSS和JS文件
所谓动态Html,指代码中组合生成的html字符串: 若需要加载本地CSS,图片,JS文件,则, 1,需要文件的全路径: 2,需要"file:///"标志: 例如: //获取文件全 ...
- js实用方法记录-js动态加载css、js脚本文件
js实用方法记录-动态加载css/js 附送一个加载iframe,h5打开app代码 1. 动态加载js文件到head标签并执行回调 方法调用:dynamicLoadJs('http://www.yi ...
- UILabel和UIbutton(富文本)封装方法
/** 方法说明:设置label的富文本属性 参数说明:contentStr富文本内容 textColor字体颜色 rangeSet设置字体颜色及大小的位置 */ - (UILabel *)backf ...
- 记录Js动态加载页面.append、html、appendChild、repend添加元素节点不生效以及解决办法
今天再优化blog页面的时候添加了个关注按钮和图片,但是页面上这个按钮和图片时有时无,本来是搞后端的,被这个前端的小问题搞得抓耳挠腮的! 网上各种查询解决方案,把我解决问题的艰辛历程分享出来,希望大家 ...
- libgdx学习记录16——资源加载器AssetManager
AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...
- UILabel加载HTML
NSString *string1 = @"<font color = \"red\">什么情况</font><br/>"; ...
随机推荐
- 解决SpringMVC拦截静态资源的问题
优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...
- Java网络编程-你是GG还是MM?
第六阶段 网络编程 每一台计算机通过网络连接起来,达到了数据互动的效果,而网络编程所解决的问题就是如何让程序与程序之间实现数据的通讯与互动 在吗?你是GG还是MM? (一) 网络模型概述 (1) 两大 ...
- [转帖]centos7设置CPU的运行频率为performance
centos7设置CPU的运行频率为performance http://www.512873.com/archives/612.html Publish: March 6, 2019 Categor ...
- [百度贴吧]飞腾1500a .VS. 龙芯3a3000: 同频实用性能对比
贴吧关于 龙芯与飞腾的简单对比. https://tieba.baidu.com/p/5682824804?red_tag=0221728732 龙芯3a3000实用性能 ,还不如4年前发布的FT15 ...
- class.forName 和 classLoader的区别
Java中的Class.forName()和ClassLoader都可以用来对类进行加载.Class.forName()除了将类的.class文件加载到JVM中 还会对类进行解释,执行类中的stati ...
- Laravel三种中间件的作用
$middleware 属性: 这个属性称为全局中间件,为什么说是全局中间件呢?因为你的每一次请求,这里面的每个中间件都会执行. $routeMiddleware 属性: 这个属性称为路由中间件,为什 ...
- python字典改变value值方法总结
今天这篇文章中我们来了解一下python之中的字典,在这文章之中我会对python字典修改进行说明,以及举例说明如何修改python字典内的值.我们开始进入文章吧. 首先我们得知道什么是修改字典 修改 ...
- Docker pull 出现的 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/
vim /etc/sysconfig/docker OPTIONS='--selinux-enabled --log-driver=journald --signature-verification= ...
- 初识机器学习——概念介绍(imooc笔记)
前言 imooc的机器学习一个最基本的介绍类课程,http://www.imooc.com/learn/717 ,不怎么涉及具体的算法或实现,只是讲了讲一些理论概念. 概述 机器学习: 利用计算机从历 ...
- __imp__SetupDiDestroyDeviceInfoList
error LINK2019 unresolved external symbol __imp__SetupDiDestroyDeviceInfoList 分类: 转载文章2012-11-02 15: ...