带有截断文本的多行 NSAttributedString
我需要一个带有多行属性文本的 UILabel subcass,并支持链接、粗体样式等。我还需要带有省略号的尾部截断。支持 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
也许我错过了一些东西,但有什么问题吗? :
这非常有效,并且会在末尾添加省略号。
Maybe I'm missing something, but whats wrong with? :
This works perfectly and will add a ellipsis to the end.
您好,我是
OHAttributedLabel
的开发者。没有简单的方法可以实现这一点(正如我在项目的 github 存储库上打开的相关问题中所解释的),因为 CoreText 不提供此类功能。
执行此操作的唯一方法是使用 CoreText 对象(CTLine 等)自行实现文本布局,而不是使用为您执行此操作的
CTFrameSetter
(但不管理行截断)。这个想法是构建所有 CTLine 来将它们一个接一个地布局(取决于它包含的 NSAttributedString 中的字形和自动换行策略),并自己管理最后的省略号。如果有人提出一个解决方案来执行此属性,我将非常感激,因为这似乎需要做一些工作,而且您还必须管理一系列特殊/不寻常的情况(表情符号情况、具有奇怪指标和不寻常字形的字体、垂直对齐、考虑到末尾省略号本身的大小来知道何时停止)。
因此,请随意挖掘并尝试自己实现线条的框架,我们将不胜感激!
Hi I am the developer of
OHAttributedLabel
.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
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 yourNSAttributedString
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!
基于我在这里和 https://groups.google.com/ 上找到的内容forum/?fromgroups=#!topic/cocoa-unbound/Qin6gjYj7XU,我想出了以下效果很好的方法。
}
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.
}
这是 @Toydor 答案的 Swift 5 版本:
Heres a Swift 5 version of @Toydor's answer:
我没有在所有情况下都尝试过这个,但类似这样的方法可能适用于截断:
I haven't tried this in all cases, but something like this could work for truncation:
多行垂直字形与截断。 Swift3 和 Swift4 版本。
新增:Xcode9.1 Swift4 兼容性。 (使用块“#if swift(>=4.0)”)
如何使用
Multi Line Vertical Glyph With Truncated. Swift3 and Swift4 version.
Add: Xcode9.1 Swift4 Compatibility. ( use block "#if swift(>=4.0)" )
How to use
您也许可以使用以下代码来获得更简单的解决方案。
我在我的项目中使用 MTLabel,这对我的项目来说是一个非常好的解决方案。
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.
如果有人感兴趣,我将 wbyoung 的解决方案集成到 OHAttributedLabel drawTextInRect: 方法中:
I integrated wbyoung's solution into OHAttributedLabel drawTextInRect: method if anyone is interested:
在 2021 年遇到了这个问题。
我将 @Toydor 和 @gypsyDev 的答案包装到 UILabel 扩展中,以便可以在需要的地方应用它。斯威夫特 5.
Ran into this issue here in 2021.
I wrapped @Toydor's and @gypsyDev's answer into a UILabel extension so it can be applied where needed. Swift 5.
我使用 MTLabel 作为示例。它允许管理行高。
我正是需要绘制方法,所以我只是收起了大部分我不需要的东西。
此方法允许我在带有尾部截断的矩形中绘制多行文本。
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.