0

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
appleBoy21
  • 632
  • 8
  • 23
  • try replacing [labelText length] with [attributedString length] on the second last line – Md. Ibrahim Hassan Nov 08 '16 at 06:30
  • @Md Ibrahim Hassan - Thanks. I did changes as per your comment. But still no change. – appleBoy21 Nov 08 '16 at 06:39
  • Note related to your issue, but note that you shouldn't get in theory the correct font (see there: http://stackoverflow.com/questions/37120535/uitextview-attributed-text-not-working-when-using-custom-font/37132592#37132592) – Larme Nov 08 '16 at 10:06
  • @Larme: with ur note, I commented "[self.labelDetails setFont:[self.labelDetails.font fontWithSize:[self.labelDescription bounds].size.height * 0.85]];" this line in viewDidLoad and replace "NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];" with "NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Gotham-Book" size:([self.labelDescription bounds].size.height * 0.85)] }];". But still not working – appleBoy21 Nov 08 '16 at 10:45
  • @Larme - To get my work done, I have to add [_paragraphStyle setFirstLineHeadIndent:0.001] line into lazy instantiation of paragraphStyle. – appleBoy21 Nov 08 '16 at 10:46
  • @Sanket, as I said, my link was not related to your issue, but fixing another issue (font part). – Larme Nov 08 '16 at 10:46
  • @Larme - but thanks for your comment, I get to know about other attributed string related issue, before I come across it. – appleBoy21 Nov 08 '16 at 10:47

1 Answers1

1

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;
Saurabh Jain
  • 1,688
  • 14
  • 28
  • thanks saurabh. Working correctly. I kept my program as it is. Just added [_paragraphStyle setFirstLineHeadIndent:0.001] line into lazy instantiation of paragraphStyle. Working great. Thanks again. But can you explain what was the problem? – appleBoy21 Nov 08 '16 at 10:12
  • @Sanket for paragraph style definition the line intent is necessary. – Saurabh Jain Nov 08 '16 at 11:19