Can't justify NSMutableAttributedString

Viewed 88

I've been using NSMutableAttributedString for a while and never ran into any issues... until now. I'm simply trying to justify some text (along with some other properties) in my label.

I tried setting the text alignment on the label itself, on the NSMutableAttributedString, I also tried to do all of that at different places (layoutSubviews, etc.) to see if it would change anything but no luck.

What I have is fairly simple:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified

let finalString = "1 500 INMILES FOR FREE\nBY ADDING YOUR BOARDING PASS"
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: finalString)

attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(.font, value: UIFont(name: "Montserrat-Bold", size: 20.0)!, range: (finalString as NSString).range(of: "1 500"))
attributedString.addAttribute(.font, value: UIFont(name: "Montserrat-Light", size: 18.0)!, range: (finalString as NSString).range(of: "INMILES FOR FREE"))

self.myLabel.attributedText = attributedString

This is the result I get:

Correct

And what I expect:

enter image description here

I've tried pretty much everything I found on SO but nothing works so far, I must be missing something...

Any help would be much appreciated!

0 Answers
Related