NSAttributedString tail truncation in UILabel
Solution 1
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)
See Word wrap for NSMutableAttributedString for further reference.
Solution 2
Following also works irrespective of using AttributedText or normal text.
Make sure to add the below line after setting the AttributedText and font to the label:
label.lineBreakMode = .byTruncatingTail
![Nikita Semenov](https://i.stack.imgur.com/gwc4g.png?s=256&g=1)
Nikita Semenov
Updated on June 13, 2022Comments
-
Nikita Semenov about 2 years
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 toattributedText
property ofUILabel
. 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!