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 ...
随机推荐
- Asp.net MVC 之异常处理
对于Asp.Net MVC 项目中,对于异常情况下,会跳转到自己定义好的页面,这时就用到了MVC中的异常过滤器(Exception Filters) (1)一旦action 方法中出现异常,异常过滤器 ...
- 钉钉的收费 [钉钉深圳研发团队 denny/2016.01.06/ 59888745@qq.com]
普通用户(个人) 团队 企业 1.免费额度为每月通话100分钟.每天发DING 5次. 1. 每月通话300分钟,每天发DING 10次. 2. 群组最多可达1500人 1. 该公司所 ...
- smarty3与2的差异导致的小问题
又是一天看视频~ 今天在PHP100上学习smartY教程,视频中讲到了在模板文件中直接写带有“{}”JAVASCRIPT脚本会报错,我照视频上的代码写了下来,如下: <script> f ...
- 鸟哥的linux私房菜学习
cat /etc/shells 系统拥有的shellcat /etc/passwd 记录用户使用的shell按两次 tab 键可显示所有可执行的指令alias 查看所有命令的别名alias lm='l ...
- requirejs笔记
1.RequireJS是一个工具库,主要用于客户端的模块管理.它可以让客户端的代码分成一个个模块.实现异步或动态加载,从而提高代码的性能和可维护性.它的模块管理遵循AMD规范(Asynchronous ...
- android studio新项目时提示:Plugin is too old, please update to a more recent version
今天想写一个程序来测试一下android studo代码,但是创建好项目后,提示: Error:(1, 0) Plugin is too old, please update to a more re ...
- 【Android 系统开发】Android JNI/NDK (三) 之 JNIEnv 解析
jni.h文件 : 了解 JNI 需要配合 jni.h 文件, jni.h 是 Google NDK 中的一个文件, 位置是 $/Android-ndk-r9d/platforms/android-1 ...
- tinyxml学习5
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...
- Bitmap 的bitmapdata和texture区别
Texture:纹理类是对不同平台不同的图片资源的封装在HTML5中,资源是一个HTMLElement对象在OpenGL / WebGL中,资源是一个提交GPU后获取的纹理idTexture类封装了这 ...
- 更好更快更高效解析JSON说明
现在来一个实例解析类,直接就把解析JSON到QVariant去了.唯一不足的是没有搞错误处理,具体方法也请各位自行参考json-c的发行文档,这样比较方便叙述,STL或者Boost我都没有认真接触过, ...