NSAttributedString tail truncation in UILabel

I am using ContextLabel to parse @, # and URL. This is the best solution I have found, so it correctly defines and does not affect performance. First, it parses the input string and converts it to NSAttributedString and then assigns the attributedText UILabel property to it. Everything works as expected, except for truncating the tail - this is very wrong (see Figure below)

enter image description here

Where should I start digging - are these the wrong attributes on the attribute string? Or a problem with the layout of labels? Thank you

+7
ios uilabel swift hashtag nsattributedstring
source share
2 answers

I had this problem and it was fixed by adding NSParagraphStyle , indicating 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 the word wrapping for NSMutableAttributedString for more details .

+14
source share

The following also works regardless of the use of the Text attribute or plain text.
Be sure to add the following line after setting the Text attribute and the font to the label:

 label.lineBreakMode = .byTruncatingTail 
0
source share

All Articles