带有截断文本的多行NSAttributedString

带有截断文本的多行NSAttributedString

本文介绍了带有截断文本的多行NSAttributedString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个带有多行属性文本的UILabel子主体,支持链接,粗体样式等。我还需要使用省略号进行尾部截断。 UILabels中没有支持属性文本的开源代码( TTTAttributedLabel OHAttributedLabel TTStyledTextLabel )似乎支持多行文本的尾部截断。有一个简单的方法来获得这个吗?

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 (TTTAttributedLabel, OHAttributedLabel, TTStyledTextLabel) seem to support tail truncation for multi-line text. Is there an easy way to get this?

推荐答案

您好我是。

没有简单的方法可以实现这一目标(如我在我的项目的github存储库上打开的相关问题),因为CoreText不提供这样的功能。

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.

执行此操作的唯一方法是实现文本布局你自己使用CoreText对象(CTLine等),而不是使用 CTFrameSetter 为你做这件事(但没有管理行截断)。我们的想法是建立所有的CTLine来布局它们(取决于它包含的 NSAttributedString 中的字形和自动换行策略)并管理省略号最后你自己。

The only way to do this would be to implement the text layout yourself using CoreText objects (CTLine, etc) instead of using the CTFrameSetter that does this for you (but w/o managing line truncation). The idea would be to build all the CTLines to lay them out (depending on the glyphs in your NSAttributedString it contains and the word wrapping policy) one after the other and manage the ellipsis at the end yourself.

我真的很感激如果有人提出一个解决方案来做这个工作,因为它似乎有点工作要做,你必须管理一系列的特殊情况或异常情况(表情符号情况,具有奇数指标的字体和不寻常的字形,垂直对齐,最后考虑省略号本身的大小以了解何时停止)。

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!

这篇关于带有截断文本的多行NSAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:39