0

I have found solution to justify text in UILabel for versions up to iOS 8.4 through attributed strings: set label string as attributed and modify hyphenation value as presented below. enter image description here

This solution stopped working on iOS 9 (text shows left aligned). I need other working solution which supports from iOS 7 or at least works at iOS 9 (would add 'if' somewhere)

Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41
  • Maybe it got removed because typographically it is considered bad practice to justify line endings. – AAGD Nov 04 '15 at 15:57
  • `NSAttributedString` works from ios 7, you could get more from [here](http://www.raywenderlich.com/50151/text-kit-tutorial) – Nghia Luong Nov 05 '15 at 11:05
  • @NghiaLuong, it is attributted at working build, but justification doesn't work anymore on iOS 9. Does it work this way only for me? – Paulius Vindzigelskis Nov 05 '15 at 12:07
  • @NghiaLuong, reading your link gave me an idea to convert my labels to textViews which allow to justify text right from nib. It is not quick fix, but at least it works. Thanks! BTW, anyone who want UITextView to automatically calculate its intrinsicSize (autolayout) - turn off scrolling – Paulius Vindzigelskis Nov 05 '15 at 13:41

1 Answers1

-1

Update:

It seems that adding a @"locale" key to attributes of NSAttributedString may help. See here.

Original answer:

You can always use NSAttributedString here:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1;
paragraphStyle.alignment = NSTextAlignmentJustified;

NSDictionary *attributes = @{
    NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody],
    NSParagraphStyleAttributeName: paragraphStyle
};

NSString *string = @"your text here";

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];

self.label.attributedText = attributedString;
self.label.numberOfLines = 0;
Community
  • 1
  • 1
bteapot
  • 1,897
  • 16
  • 24
  • I use attributted string as provided in screenshot. Only difference is that I did that in XIB, not in code. But as I mentioned in questions - it doesn't work in iOS 9. It shows as left aligned – Paulius Vindzigelskis Nov 11 '15 at 15:24
  • @PooLaS Interesting that when I supply text in my local language – Russian – it appears justified and hyphenated, but in English it's left-aligned and non-hyphenated. – bteapot Nov 11 '15 at 15:54