Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextAlignmentJustified not working in iOS7

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

like image 648
markusNetural Avatar asked Oct 08 '13 15:10

markusNetural


3 Answers

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]];
like image 138
markusNetural Avatar answered Oct 21 '22 17:10

markusNetural


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];
like image 37
βhargavḯ Avatar answered Oct 21 '22 16:10

βhargavḯ


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;
like image 27
Lord Kale XI Avatar answered Oct 21 '22 16:10

Lord Kale XI