Sanket Sanket - 1 month ago 13
Objective-C Question

Not getting justified UILabel attributed text properly

I have one uilabel into my iOS app. I applied attributed text to UIlabel with paragraph style. But I didn't getting why justification not gets applied for first 4-5 lines? Then onwards all prints well (see below screenshot). Please suggest what I'm doing wrong.

PROBLEM

enter image description here

MY CODE - DetailsViewController.m

#import "DetailsViewController.h"

@interface DetailsViewController ()

@property (weak, nonatomic) IBOutlet UILabel *labelDescription;
@property (weak, nonatomic) IBOutlet UILabel *labelDetails;
@property (strong, nonatomic) NSMutableParagraphStyle *paragraphStyle;

@end

@implementation DetailsViewController

#pragma mark - lazy instantiation

- (NSMutableParagraphStyle *)paragraphStyle {
if (!_paragraphStyle) {
_paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[_paragraphStyle setAlignment:NSTextAlignmentJustified];
[_paragraphStyle setHyphenationFactor:1.0f];
[_paragraphStyle setLineSpacing:5.0f];
}
return _paragraphStyle;
}

#pragma mark - view controllers life cycle methods

- (void)viewDidLoad {
[super viewDidLoad];

[self.view layoutIfNeeded];

// updating fonts
[Utils updateLabelFontSize:self.labelDescription ForInitialHeight:20 andInitialSize:18];
[self.labelDetails setFont:[self.labelDetails.font fontWithSize:[self.labelDescription bounds].size.height * 0.85]];

// create labelText to.hFile
NSString *labelText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];

[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [labelText length])];
[self.labelDetails setAttributedText:attributedString];
}

@end

Answer

Try this code:

NSMutableParagraphStyle *ParagraphStyle = [[NSMutableParagraphStyle alloc] init];
    ParagraphStyle.alignment = NSTextAlignmentJustified;
    ParagraphStyle.firstLineHeadIndent = 0.001;
    ParagraphStyle.lineSpacing = 5.0;
   NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:labeltext attributes:@{NSParagraphStyleAttributeName:ParagraphStyle}];
self.labelDetails.AttributedText = attStr;