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 ...
随机推荐
- Hbase预分区种子生成
提前生成Hbase预分区种子,在创建Hbase表时也进行相应的预分区,同时设置预分区的个数,预分区的范围对应Hbase监控页面的Region Server的start key与End key,从而使数 ...
- Linux下*.tar.gz文件解压缩命令
1.压缩命令: 命令格式:tar -zcvf 压缩文件名.tar.gz 被压缩文件名 可先切换到当前目录下.压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式:tar -z ...
- 让finder显示路径
在控制台输入 defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES 重启finder即可.
- 仿淘宝详情转场(iOS,安卓没有这功能)
由于公司是做跨境电商的,所以对各大电商APP都有关注,最近看到淘宝iOS端(安卓没有)在商品详情点击加入购物车有一个动画效果特别赞,正好今天新版本上线,下午就抽了些时间研究了下. 主要思路是自定义转场 ...
- pgadmin中的备份功能消失的原因和找回方法
pgadmin在正常情况下,在左侧的[对象浏览器]中选中数据库.模式或者表时,右键会出现[备份]和[恢复]的选项(除早期不支持该功能的版本外). 如: 当然,有时候有人会发现,当你想要用这些备份还原功 ...
- 关于新闻,在线编辑器建表时此字段一定要为text
create table about( content text )engine=myisam default charset=utf8; 项目的各个建表语句 create database day4 ...
- sqlite支持各种交集差集 并集操作了
- 比较一下Linux下的Epoll模型和select模型的区别
一. select 模型(apache的常用) 1. 最大并发数限制,因为一个进程所打开的 FD (文件描述符)是有限制的,由 FD_SETSIZE 设置,默认值是 1024/2048 ,因此 Sel ...
- MySQL的表的优化和列类型的选择
列选择原则: 1:字段类型优先级 整型 > date,time > enum,char>varchar > blob 列的特点分析: 整型: 定长,没有国家/地区之分,没有 ...
- MC的缓存雪崩现象和缓存无底洞的原因以及导致的后果的总结
缓存雪崩一般是由某个缓存节点失效,导致其他节点的缓存命中率下降,缓存中确实的数据去数据库查询,短时间内,造成数据库服务器的崩溃. 这时,我们需要重启数据库,但重启一段时间后,又会被压垮,但此时缓存的数 ...