UILabel中的NSAttributedString尾部截断 [英] NSAttributedString tail truncation in UILabel
问题描述
我正在使用 ContextLabel 来解析@,#和URL.这是我发现的最佳解决方案,可正确调整尺寸且不影响性能.它首先解析输入中的字符串,然后将其转换为NSAttributedString
,然后将其分配给UILabel
的attributedText
属性.一切都按预期工作,除了尾部截断-这是非常不正确的(请参见下图)
I'm using ContextLabel to parse @ , # and URL's. This is the best solution i found, cause it sizes correctly and dont affect performance. It firstly parses string at input and than converts it to NSAttributedString
and after this assigns it to attributedText
property of UILabel
. Everything works as expected, except tail truncation - it's very incorrect ( see pic below )
我应该从哪里开始挖掘-属性字符串的属性是否错误?还是标签布局问题?谢谢!
Where shall i start digging - is it wrong attributes on attributed string? Or label layout issue? Thanks!
推荐答案
我遇到了此问题,并通过添加NSParagraphStyle
指定所需的换行模式来解决此问题:
I had this problem and fixed it by adding a NSParagraphStyle
specifying the desired line break mode:
//assuming myString is an NSMutableAttributedString
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail
let range = NSRange(location: 0, length: myString.mutableString.length)
myString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
请参见 NSMutableAttributedString的自动换行以获取更多参考.
See Word wrap for NSMutableAttributedString for further reference.
这篇关于UILabel中的NSAttributedString尾部截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!