10

I am use Xcode4.5 and iOS6 now, the iOS6 have change the UILable's textAlignment,

label.textAlignment = NSTextAlignmentJustified;

The code will crash on iPhone 6.0 Simulator with ios6 SDK. but not crash on iPhone 5.0 Simulator with iOS6 SDK.

The iOS6 support NSTextAlignmentJustified, but why will crash?

user501836
  • 1,106
  • 1
  • 12
  • 20
  • Can you post what the exception is? – Léo Natan Dec 24 '12 at 22:47
  • same error. Error log is : `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textAlignment does not accept NSTextAlignmentJustified'` – Martin Dec 27 '12 at 10:39

2 Answers2

10

Edit 1:

Using NSAttributedString we can justify text.

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment                = NSTextAlignmentJustified;
paragraphStyles.firstLineHeadIndent      = 0.05;    // Very IMP

NSString *stringTojustify                = @"We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far.";
NSDictionary *attributes                 = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString     = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes];

self.lblQuote.attributedText             = attributedString;
self.lblQuote.numberOfLines              = 0;
[self.lblQuote sizeToFit];

enter image description here

Deprecation of UITextAlignmentJustify, not available anymore under iOS 6.

All possible solutions to justified text are given at this SO link also have a look at this page and iOS6 prerelease Documentation.

Community
  • 1
  • 1
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • 1
    NSAttributedString does not have a method setTextAlignment:lineBreakMode:. That method is part of a third party UILabel subclass. – puzzl Sep 04 '13 at 19:32
  • 1
    Actually setting the firstLineHeadIndent property is what blocks the ability of the uilabel to adjust the font size to fit the bounds. What you need to set is either the headIndent or trailIndent properties, or insert the NSBaselineOffsetAttributeName attribute as pointed by poloDelaVega below here – Jim75 Aug 18 '15 at 14:06
5

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

No idea why you need to add this baseline setting on iOS 7, (it works fine on iOS 6).

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentJustified;

NSDictionary *attributes = @{ NSParagraphStyleAttributeName : paragraph,
                                        NSFontAttributeName : self.textLabel.font,
                              NSBaselineOffsetAttributeName : [NSNumber numberWithFloat:0] };

NSAttributedString *str = [[NSAttributedString alloc] initWithString:content 
                                                          attributes:attributes];

self.textLabel.attributedText = str;

Hope that helps ;)

poloDelaVega
  • 1,145
  • 1
  • 9
  • 5
  • Very good!!! Running properly. You can exchange `NSBaselineOffsetAttributeName : [NSNumber numberWithFloat:0]` to `NSBaselineOffsetAttributeName : @0` – Matheus Abreu Feb 26 '14 at 00:22