利用YYLabel 进行图文混排+高度计算

1、项目需求:

用一个控件显示图片和文字,并且依据图片和文字动态计算控件的高度。

2、方案:

利用YYLabel控件和属性字符串处理。

注:(在使用YYLabel之前,使用UILabel测试过,但是发现在图文混排的时候。利用属性字符串计算高度不太准确。会有多余的文字不显示。)

示例代码

//使用YYText 处理富文本行高

YYLabel *contentL = [[YYLabel alloc] init];
//设置多行
contentL.numberOfLines = ;
//这个属性必须设置,多行才有效
contentL.preferredMaxLayoutWidth = kScreenWidth -; NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithAttributedString:[OSCBaseCommetView contentStringFromRawString:commentItem.content withFont:24.0]]; //可以将要插入的图片作为特殊字符处理
//需要使用 YYAnimatedImageView 控件,直接使用UIImage添加无效。 YYAnimatedImageView *imageView1= [[YYAnimatedImageView alloc] initWithImage:[UIImage imageNamed:@"ic_quote_left"]];
imageView1.frame = CGRectMake(, , , ); YYAnimatedImageView *imageView2= [[YYAnimatedImageView alloc] initWithImage:[UIImage imageNamed:@"ic_quote_right"]];
imageView2.frame = CGRectMake(, , , );
// attchmentSize 修改,可以处理内边距
NSMutableAttributedString *attachText1= [NSMutableAttributedString attachmentStringWithContent:imageView1 contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView1.frame.size alignToFont:[UIFont systemFontOfSize:] alignment:YYTextVerticalAlignmentCenter]; NSMutableAttributedString *attachText2= [NSMutableAttributedString attachmentStringWithContent:imageView2 contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView2.frame.size alignToFont:[UIFont systemFontOfSize:] alignment:YYTextVerticalAlignmentCenter]; //插入到开头
[attri insertAttributedString:attachText1 atIndex:];
//插入到结尾
[attri appendAttributedString:attachText2]; //用label的attributedText属性来使用富文本
contentL.attributedText = attri; CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - , MAXFLOAT); //计算文本尺寸
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
contentL.textLayout = layout;
CGFloat introHeight = layout.textBoundingSize.height; contentL.frame = commentItem.layoutInfo.contentTextViewFrame;
contentL.width = maxSize.width; contentL.height = introHeight + ; [self addSubview:contentL];

利用YYLabel 进行图文混排+高度计算的更多相关文章

  1. 利用NSAttributedString实现图文混排

    UILabel 和 UITextView 都能添加 NSAttributedString 属性字符串,通过这一点,可以实现带有属性的文字和文字内包含图片的文本内容展示. 效果如下:   1-初始化可变 ...

  2. Android TextView中图文混排设置行间距导致高度不一致问题解决

    最近项目中需要实现一个评论带表情的功能,刚开始一切顺利,非常easy,突然有一天发现文字跟表情混排的时候,TextView中图文高度不一致,excuse...什么鬼,之前明明测试过图文混排,不存在这个 ...

  3. 简单的Coretext 图文混排

    在很多新闻类或有文字展示的应用中现在都会出现图文混排的界面例如网易新闻等,乍一看去相似一个网页,其实这样效果并非由UIWebView 加载网页实现.现在分享一种比较简单的实现方式 iOS sdk中为我 ...

  4. CoreText实现图文混排

    CoreText的介绍 Core Text 是基于 iOS 3.2+ 和 OSX 10.5+ 的一种能够对文本格式和文本布局进行精细控制的文本引擎.它良好的结合了 UIKit 和 Core Graph ...

  5. 【iOS】使用CoreText实现图文混排

    iOS没有现成的支持图文混排的控件,而要用多个基础控件组合拼成图文混排这样复杂的排版,是件很苦逼的事情.对此的解决方案有使用CoreText进行绘制,或者使用TextKit.本文主要讲解对于CoreT ...

  6. android开发 自定义图文混排控件

    功能:图文混排,可自动缩放字体,如图: 单点触控使用的代码来自:http://blog.csdn.net/xiaanming/article/details/42833893  谢谢博主! 在该dem ...

  7. Android 自绘TextView解决提前换行问题,支持图文混排

    先看下效果图: 上面是MTextView,下面是默认的TextView. 一.原因 用最简单的全英文句子为例,如果有一个很长的单词,这一行剩余的空间显示不下了,那么规则就是不打断单词,而是把整个单词丢 ...

  8. CoreText实现图文混排之点击事件-b

    CoreText实现图文混排之点击事件 主要思路 我们知道,CoreText是基于UIView去绘制的,那么既然有UIView,就有 -(void)touchesBegan:(NSSet<UIT ...

  9. CoreText 实现图文混排

    CoreText 实现图文混排 相关博文推荐 IOS CoreText.framework - 基本用法 IOS CoreText.framework - 段落样子CTParagraphStyle h ...

随机推荐

  1. leetCode(51):Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. 浅谈 SOAP

    http://www.ibm.com/developerworks/cn/xml/x-sisoap/ 本文对 SOAP 作了一个初步介绍,给出几个简单示例:接着比较 CORBA,DCOM/COM 与 ...

  3. UITableView基础入门

    新建一个Single View Application 添加一个空类如下: using System; using UIKit; using Foundation; namespace BasicTa ...

  4. Leetcode 001-twosum

    #Given an array of integers, return indices of the two numbers such that they add up to a specific t ...

  5. 【BZOJ4296】[PA2015]Mistrzostwa BFS

    [BZOJ4296][PA2015]Mistrzostwa Description 给定一张n个点m条边的无向图,请找到一个点数最多的点集S,满足:1.对于点集中任何一个点,它至少与d个点集中的点相邻 ...

  6. 可以执行全文搜索的原因 Elasticsearch full-text search Kibana RESTful API with JSON over HTTP elasticsearch_action es 模糊查询

    https://www.elastic.co/guide/en/elasticsearch/guide/current/getting-started.html Elasticsearch is a ...

  7. 2017NOIP游记 (格式有点炸)

    NOIP游记 作者:一只小蒟蒻 时间可真快呀!还记得我第一次接触信息竞赛时,hello world都要调好久,不知不觉就考完了2017noip,自我感觉良好(虽然还是有很多不足). 这两个月的闭关,让 ...

  8. [Java SE] 字符串连接

    Java 支持多种字符串连接方式,总结如下: package cn.spads.tool.string; import java.text.MessageFormat; /** * <b> ...

  9. Vue.js的动态组件模板

    组件并不总是具有相同的结构.有时需要管理许多不同的状态.异步执行此操作会很有帮助. 实例: 组件模板某些网页中用于多个位置,例如通知,注释和附件.让我们来一起看一下评论,看一下我表达的意思是什么.评论 ...

  10. LightOJ1236 —— 唯一分解定理 + 最小公倍数

    题目链接:https://vjudge.net/problem/LightOJ-1236 1236 - Pairs Forming LCM    PDF (English) Statistics Fo ...