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 ...
随机推荐
- Hadoop I/O操作原理整理
I/O操作中的数据检查 校验和方式是检查数据完整性的重要方式.一般会通过对比新旧校验和来确定数据情况,如果两者不同则说明数据已经损坏.比如,在传输数据前生成了一个校验和,将数据传输到目的主机时再次计算 ...
- java中Date与String的相互转化
1:大体思路 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new Date("2008-04 ...
- List集合中的对象根据属性排序
集合类List存放的数据,默认是按照放入时的顺序存放的,比如依次放入A.B.C,则取得时候,则也是A.B.C的顺序,实际场景中,有时我们需要根据自定义的规则对List中的元素进行排序,该如何实现呢?看 ...
- 谈谈我对DSP和FPGA的看法
1.DSP 在DSP里,你是一个软件设计者,硬件已经完全固化,你所要做的,就是在这个固定的硬件平台实现算法改进与优化, DSP的关键优势在于能够运行多种算法的灵活性: 2.FPGA 对于FPGA来说, ...
- Distributed Transaction Coordinator 无法启动
有时候我们需要进行COM应用程序的权限设置,控制面板-->管理工具-->组件服务-->然后依此展开:组件服务-->计算机-->我的电脑-->DCOM 配置,接下来找 ...
- tiny学习3
这几天在埋头写自己的个星期!而且由于它是基于事件发生的次序(小时就把我的文件导出来了--呵呵.在阅读本文之前,请先看看我Blog里转贴的<TinyXML学习笔记>,相信它能给各位一个关于T ...
- ubuntu 13.04 lighttped mysql php
apt-get update sudo apt-get install lighttpd php5-cgi Enable the fastcgi module and the php configur ...
- C++中的RTTI机制解析
RTTI RTTI概念 RTTI(Run Time Type Identification)即通过运行时类型识别,程序能够使用基类的指针或引用来检查着这些指针或引用所指的对象的实际派生类型. RTTI ...
- PIC32MZ tutorial -- 32-bit Timer
The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...
- 浅析Linux下的/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc文件
转自:http://www.ahlinux.com/shell/20239.html 0x01 /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,并从/e ...