html图片自适应div大小_未知宽高的div元素垂直水平居中

html图片自适应div大小_未知宽高的div元素垂直水平居中1.设置label的html图片-(NSMutableAttributedString*)setAttributedString:(NSString*)str{//如果有换行,把\n替换成<br/>//如果有需要把换行加上str=[strstringByReplacingOccurrencesOfString:@”\n”withString:@”<br/>”];//设置HTML图片的宽度str=[NSString

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

1.设置label的html图片

-(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-28,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:14]} range:NSMakeRange(0, htmlString.length)];
    //设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [htmlString length])];

    return htmlString;
}

2.设置html图片的高度
计算出来的 height 正好是排版后的高度大小,是 CGFloat 类型,在是在我们设置UIlabel/Cell 高度时,可能存在四舍五入等,最后存在的一点点误差使得 UILabel 显示不全,可能出现缺少一行,上下空白太多等情况;

解决方案:为了确保布局按照我们计算的数据来,可以使用ceil函数对计算的 Size 取整,再加1,确保 UILabel按照计算的高度完好的显示出来; 或者使用方法CGRectIntegral(CGRect rect) 对计算的 Rect 取整,在加1;

-(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:14]} range:NSMakeRange(0, htmlString.length)];
    //设置行间距
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:5];
    [htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [htmlString length])];

//    CGSize contextSize = [htmlString boundingRectWithSize:(CGSize){[UIScreen mainScreen].bounds.size.width-28, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    CGSize labelSize = [_detailLab sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width-28, MAXFLOAT)];
    CGFloat height = ceil(labelSize.height) + 1;
    return height;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189367.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vue(22)Vuex的安装与使用

    vue(22)Vuex的安装与使用前言每一个Vuex应用的核心就是store(仓库)。store基本上就是一个容器,它包含着你的应用中大部分的状态(state)。Vuex和单纯的全局对象有以下两点不同:Vuex的状态存

    2022年7月30日
    6
  • 通过pycharm安装python_JAVA开发环境

    通过pycharm安装python_JAVA开发环境Python开发环境搭建与helloWorld测试1.去官网下载然后傻瓜式安装2.下载开发IDE:这里选用pychram下载地址:pychram官网新建一个工厂后写简单的helloworld然后:找到你工程的文件,Helloworld.py最后点击OK即可看运行结果:pycha…

    2022年8月27日
    3
  • XML解析

    XML解析一、邂逅XML文件种类是丰富多彩的,XML作为众多文件类型的一种,经常被用于数据存储和传输。所以XML在现今应用程序中是非常流行的。本文主要讲Java解析和生成XML。用于不同平台、不同设备间的数据

    2022年6月30日
    28
  • 堆栈的应用——用JavaScript描述数据结构[通俗易懂]

    堆栈的应用——用JavaScript描述数据结构[通俗易懂]堆栈的应用——用JavaScript描述数据结构

    2022年4月20日
    37
  • matplotlib的安装教程以及简单调用

    matplotlib的安装教程以及简单调用1.matplotlib的下载我们的常规下载方式就是在命令行中输入:`pipinstallmatplotlib`,这样你就可以从官方进行下载,但是这样的下载速度是十分的慢的,我们在最详细的AnacondaInstallers的安装【numpy,jupyter】(图+文)(https://chen-ac.blog.csdn.net/article/details/122374025?spm=1001.2014.3001.5502)这一博客中曾写到,可以在`pipinstallmatplo

    2022年6月15日
    56
  • Java安全之Weblogic内存马

    Java安全之Weblogic内存马0x00前言发现网上大部分大部分weblogic工具都是基于RMI绑定实例回显,但这种方式有个弊端,在WeblogicJNDI树里面能将打入的RMI后门查看

    2021年12月13日
    50

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号