12

I have an app which uses NSTextAlignmentJustified for an NSAttributedString. In iOS 6 everything is working great. But the same App running in iOS 7 (simulator or device makes no difference) is showing no Justify at all. Also the linespacing seems to have changed dramatically from iOS 6 to 7.

Anyone else encountered this problem? Is there any way to make a justified Textblock in iOS 7 (which works in iOS 6 too?)

Regards, Markus

aksh1t
  • 5,410
  • 1
  • 37
  • 55
markusNetural
  • 391
  • 3
  • 6
  • I too have found this problem with UILabel. I'll look into it more for you-really want my text justified! – Rambatino Oct 12 '13 at 18:04

3 Answers3

27

Ok, i kind of found a way to make the label Justifiy in iOS 7: i simply set NSBaselineOffsetAttributeName to 0.

No idea why it works, but it works.

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentJustified;

NSAttributedString *string = [[NSAttributedString alloc] initWithString:rawString 
          attributes:[NSDictionary dictionaryWithObjectsAndKeys:
          paragraphStyle, NSParagraphStyleAttributeName , 
          [NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName, 
          nil]];
markusNetural
  • 391
  • 3
  • 6
  • I thought NSBaselineOffsetAttributeName was deprecated in ios6? https://developer.apple.com/LIBRARY/ios/releasenotes/General/RN-iOSSDK-6_0/index.html Due to compatibility concerns, the NSBaselineOffsetAttributeName attribute is no longer supported in iOS 6. – Sean Dunford Apr 09 '14 at 19:38
  • It's not deprecated! https://developer.apple.com/reference/uikit/nsbaselineoffsetattributename – Bisca Aug 30 '16 at 12:06
8

Setting firstLineHeadIndent on NSMutableParagraphStyle will also work too.

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment                = NSTextAlignmentJustified;    // To justified text
paragraphStyles.firstLineHeadIndent      = 0.05;    // IMP: must have a value to make it work

NSString *stringTojustify                = @"No one wakes up excited to see more advertising, no one goes to sleep thinking about the ads they’ll see tomorrow.";
NSDictionary *attributes                 = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString     = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes];

self.lblQuote.attributedText             = attributedString;
self.lblQuote.numberOfLines              = 0;
[self.lblQuote sizeToFit];
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
1

Just for the record, you could also append a '\n' as first character for a plain UILabel.

        self.text = [NSString stringWithFormat:@"\n%@",TextString];
        {
            CurFrame.origin.y -= FontSize;
            self.frame = CurFrame;
        }
        self.textAlignment = NSTextAlignmentJustified;
  • Nice, but is there a way to avoid the initial empty line without switching to NSAttributedString? – Cœur Aug 03 '15 at 03:54
  • 1
    afaik \f (Page Break) could do cleanly without having to resize frame, but it's still an hack. Chances are that it's worth the effort to learn how NSAttributedString works if you need Justified, because sooner or later you need them AND Apple might change behaviour at some point (it all depends about what the code really do behind for having this thing to work, which I'm not bothered to check anyway). – Lord Kale XI Aug 03 '15 at 12:16