Multi-line NSAttributedString with truncated text
http://stackoverflow.com/questions/7611816/multi-line-nsattributedstring-with-truncated-text/10172794#10172794


| I need a UILabel subcass with multiline attributed text with support for links, bold styles, etc. I also need tail truncation with an ellipsis. None of the open source code that supports attributed text inside UILabels ( | ||||
| Hi I am the developer of  There is no easy way to achieve this (as explained in the associated issue I opened on the github repository of my project), because CoreText does not offer such feature. The only way to do this would be to implement the text layout yourself using CoreText objects (CTLine, etc) instead of using the  I would really appreciate if someone propose a solution to do this propery as it seems a bit of work to do and you have to manage a range of special/unusual cases too (emoji cases, fonts with odd metrics and unusual glyphs, vertical alignment, take into account the size of the ellipsis itself at the end to know when to stop). So feel free to dig around and try to implement the framing of the lines yourself it would be really appreciated! | |||||||||||||||||
| 
 | 


| Based on what I found here and over at https://groups.google.com/forum/?fromgroups=#!topic/cocoa-unbound/Qin6gjYj7XU, I came up with the following which works very well. } | |||||||||||||||||||||
| 
 | 
| Maybe I'm missing something, but whats wrong with? : This works perfectly and will add a ellipsis to the end. | |||||
| 
 | 
| I haven't tried this in all cases, but something like this could work for truncation:  | |||||
| 
 | 
| You may be able to use follow code to have a more simple solution. I'm using MTLabel in my project and it's a really nice solution for my project. | ||||
| I integrated wbyoung's solution into OHAttributedLabel drawTextInRect: method if anyone is interested:  | |||
| I used as sample MTLabel. It allows to manage line height. I needed the draw method exactly, so i just put away most stuff i did not need. This method allows me to draw multilined text in rect with tail truncation.  | |||
if (true&&self.lineHeights>0 && CFArrayGetCount(lines)>=2) {
        NSUInteger lineCount = CFArrayGetCount(lines);
        CGPoint *origins = malloc(sizeof(CGPoint) * lineCount);
        CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), origins);
        CTLineRef line0 = (CTLineRef)CFArrayGetValueAtIndex(lines, 0);
        CTLineRef line1 = (CTLineRef)CFArrayGetValueAtIndex(lines, 1);
NSAttributedString *truncatedString = [[NSAttributedString alloc]initWithString:@"\u2026"];
        CTLineRef token = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)truncatedString);
        CTLineTruncationType ltt = kCTLineTruncationEnd;
        CTLineRef  line = CTLineCreateTruncatedLine(line1, self.width-10, ltt, token);
        CGPoint point = origins[0];
        CGContextSetTextPosition(context, point.x, point.y);
        CTLineDraw(line0, context);
        point = origins[1];
        CGContextSetTextPosition(context, point.x, point.y);
        CTLineDraw(line, context);
        //CTFrameDraw(frame, context);
        free(origins);
}
Multi-line NSAttributedString with truncated text的更多相关文章
- css & multi line words &  ellipsis
		css & multi line words & ellipsis bug .news-card-content-title { width: 100%; height: 0.8rem ... 
- Python使用XML操作mapnik,实现复杂标注(Multi line text symbolizer)
		test.py import mapnik stylesheet = 'world_style.xml' image = 'world_style.png' m = mapnik.Map(1200, ... 
- Display certain line(s) from a text file in Linux.
		Purpose: Display certain line or lines from a text file, such as : Display the 1000th line from file ... 
- Creating fields using CSOM
		When creating a field, whether you are using CAML, server-side object mode, or one of the client-s ... 
- create feature from text file
		'''---------------------------------------------------------------------------------- Tool Name: Cre ... 
- [转帖]Introduction to text manipulation on UNIX-based systems
		Introduction to text manipulation on UNIX-based systems https://www.ibm.com/developerworks/aix/libra ... 
- 78k的text 文件,输出到textbox 竟然用了20几秒的原因
		开始查资料以为是io读取的,磁盘速度慢的之类的,后来一想,text 又不是几十万条数据,才那么两千行,咋回事. 原来是循环中不停的修改textbox值导致的. 总结: 比较大的运算,特别是大的循环, ... 
- In line copy and paste to system clipboard
		On the Wiki Wiki Activity Random page Videos Photos Chat Community portal To do Contribute Watch ... 
- CSU1019: Simple Line Editor
		1019: Simple Line Editor Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Subm ... 
随机推荐
- Intellij Idea系列之Tomcat环境的搭建(三)
			Intellij Idea系列之Tomcat环境的搭建(三) 一. 编写背景 Intellij Idea在刚上手的时候很多人吐槽,"god, 这么难用的IDE有谁用呀?",的确,I ... 
- 【转】关于Block Formatting Context--BFC和IE的hasLayout
			转自穆乙 http://www.cnblogs.com/pigtail/ 一.BFC是什么? BFC(Block Formatting Context)直译为“块级格式化范围”. 是 W3C CSS ... 
- Storm Bolt接口
			Bolt是Topology中数据处理的基本单元,也是Storm针对处理过程的编程单元.Topology中所有的处理都是在这些bolt中完成的. Bolt可以将数据项发送至多个数据流(Str ... 
- Storm Topology及分组原理
			Storm的通信机制,需要满足如下一些条件以满足Storm的语义. 1.建立数据传输的缓冲区.在通信连接没有建立之前把发送的数据缓存起来.数据发送方可以在连接建立之前发送消息,而不需要等连接建立起来, ... 
- mysql按月获取一年内每个月的数据量
			SELECT COUNT(id) AS num, DATE_FORMAT(create_time, '%Y-%m') AS createTime FROM table WHERE create_tim ... 
- HTML音乐播放——切歌
			<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ... 
- Flex 加载 wmf,svg
			最近做gis系统发现要在flex加载wmf图片.我记得flash的loader只能是png,gis,jpg.wmf格式居然是window出的,flash居然不支持我也是醉了,没办法,只能后台转格式,首 ... 
- CSS3属性transition
			CSS3 Transitions 指定过渡 语法: transition: property duration timing-function delay; 参数一: transition-p ... 
- android 程序开机自启动
			今天遇到程序开机自启动,然后查了一下,很简单,就记录一下. 开机自启动,一般我们是开启启动一个广播,然后在广播里启动Activity或者别的服务. 我们要做的很简单,就是在AndroidManifes ... 
- java泛型中的super和extend
			List<? extend Fruit> list=new ArrayList<>(); 解释为:集合中元素是继承自Fruit,究竟是何种类型,编译器也无法判定. 如果要从集 ... 
