How to justify the label text?

My code for aligning the label text is below: -

-(void)justifyToLabel:(UILabel*)label withStr:(NSString*)str{
    NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
    paragraphStyles.alignment = NSTextAlignmentJustified;
    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: str attributes: attributes];
    label.attributedText = attributedString;
}

I do not know why it does not work. Please give the best solution.

thank

+4
source share
1 answer

Try the code below:

You have not assigned a value to the property firstLineHeadIndent, so it does not work.

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
paragraphStyles.firstLineHeadIndent = 1.0;                //must have a value to make it work

NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];

label.attributedText = attributedString;
+6
source

All Articles