2

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!

mxlhz
  • 1,033
  • 11
  • 21
  • try adding a new line at the end – Leo Dabus Nov 09 '20 at 22:47
  • Thanks for the suggestion Leo, I just tried that but still the same unfortunately. – mxlhz Nov 09 '20 at 23:07
  • try setting `kCTBaselineOffsetAttributeName` to zero https://stackoverflow.com/a/27060631/2303865 – Leo Dabus Nov 09 '20 at 23:41
  • Thanks again for the suggestion Leo, I found that post before posting my question and unfortunately it didn't work either. I tried a again just to make sure, but still no luck... – mxlhz Nov 10 '20 at 00:24
  • What iOS are you using? I had some issues with IOS 14 and 14.1 to strike through Attributed String. – Leo Dabus Nov 10 '20 at 00:30
  • I'm currently debugging on iOS 14.1 and tested on a device running iOS 13.3. Was there anything you had to do to make it work? – mxlhz Nov 10 '20 at 00:36
  • the baseline set to zero fixed it `attrString.addAttribute(.baselineOffset, value: 0,` – Leo Dabus Nov 10 '20 at 00:37
  • I tried setting that with `attributedString.addAttribute(.baselineOffset, value: NSNumber(value: 0), range: NSMakeRange(0, finalString.length))`, but it didn't change a thing. I'm really wondering what's going on here. Did you set any other attributes beside `.baselineOffset` and `.strikethroughStyle` or specific constraints on your label? – mxlhz Nov 10 '20 at 00:50
  • I can't really test your code as I don't have that font on my system – Leo Dabus Nov 10 '20 at 00:54
  • The constraints it is definitely not an issue – Leo Dabus Nov 10 '20 at 00:55
  • 1
    There is a possible hack is to add `_____________________________________` at the end of the second line and set the numberOfLines to 2 – Leo Dabus Nov 10 '20 at 01:00
  • Ok, so it seems to do the trick for the second line, which is great! But when I test on bigger screens like an iPhone 12 Pro Max, I now have the same issue but with the first line this time. I might have to split this label into 2 labels and do the same trick for both I guess... I'll try that after getting some sleep and see how it goes, thanks a lot Leo, your help is very much appreciated! – mxlhz Nov 10 '20 at 01:50
  • Yes. I knew that would happen. The solution is definitely to use 2 labels. – Leo Dabus Nov 10 '20 at 02:05

0 Answers0