magohamote
magohamote

Reputation: 1494

NSTextAlignment.Justified for UILabel does not work

I am trying to justify my UILabel text but it does not work.

Declaration of my UIView:

descriptionUIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

Declaration of my UILabel:

bottleDescriptionLabel = UILabel(frame: CGRect(x: widthMargin, y: bottleDescriptionTitleLabel.frame.maxY + heightMargin, width: self.view.frame.width - (2 * widthMargin), height: heightBottleDescription - (2 * heightMargin)))
bottleDescriptionLabel.font = UIFont(name: "AvenirNext-Regular", size: 16)
bottleDescriptionLabel.text = bottleDescriptionString
bottleDescriptionLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
bottleDescriptionLabel.textAlignment = NSTextAlignment.Justified
bottleDescriptionLabel.numberOfLines = 0

And it looks like this:

enter image description here

I don't know what else to use that NSTextAlignment.Justified to justified my text. Should I use a UITextView instead?

Upvotes: 9

Views: 16826

Answers (5)

Piyush Sanepara
Piyush Sanepara

Reputation: 1417

func justifyLabel(str: String) -> NSAttributedString
{
   let paragraphStyle = NSMutableParagraphStyle()
   paragraphStyle.alignment = NSTextAlignment.justified
   let attributedString = NSAttributedString(string: str,
                                         attributes: [
              NSAttributedString.Key.paragraphStyle: paragraphStyle,
              NSAttributedString.Key.baselineOffset: NSNumber(value: 0)
        ])

   return attributedString
}

call justifyLabel() function like this...

myLabel.attributedText = justifyLabel(myLabel.text!)

Upvotes: 4

Syed Asad Ali Kazmi
Syed Asad Ali Kazmi

Reputation: 27

I have been facing this and looking for answer, and I use only this line instead of paragraphStyle or NSAttributedString.

self.labelDescription.textAlignment = .justified

Upvotes: 1

Ahmed Abdallah
Ahmed Abdallah

Reputation: 2355

Objective-C, the perfect update solution is to used NSMutableParagraphStyle test on xCode 7 and iOS 9

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
        paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
        paragraphStyles.firstLineHeadIndent = 1.0;
        NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: YourString attributes: attributes];
        YourLabel.attributedText = attributedString;

Upvotes: -1

Devran Cosmo Uenal
Devran Cosmo Uenal

Reputation: 6205

You have to create an NSMutableParagraphStyle in combination with an NSAttributedString in order to display text as justified. The important part is to set NSBaselineOffsetAttributedName to 0.0.

Here's an example how to put everything together:

let sampleText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified

let attributedString = NSAttributedString(string: sampleText,
    attributes: [
        NSParagraphStyleAttributeName: paragraphStyle,
        NSBaselineOffsetAttributeName: NSNumber(float: 0)
    ])

let label = UILabel()
label.attributedText = attributedString
label.numberOfLines = 0
label.frame = CGRectMake(0, 0, 400, 400)


let view = UIView()
view.frame = CGRectMake(0, 0, 400, 400)
view.addSubview(label)

Credits for NSBaselineOffsetAttributedName: https://stackoverflow.com/a/19445666/2494219

Upvotes: 30

Mayank Gupta
Mayank Gupta

Reputation: 125

You can use UITextView for your problem.

bottleDescriptionTextView = UITextView(frame: CGRect(x: widthMargin, y: bottleDescriptionTextView.frame.maxY + heightMargin, width: self.view.frame.width - (2 * widthMargin), height: heightBottleDescription - (2 * heightMargin)))
bottleDescriptionTextView.font = UIFont(name: "AvenirNext-Regular", size: 16)
bottleDescriptionTextView.text = bottleDescriptionString
bottleDescriptionTextView.textAlignment = NSTextAlignment.Justified

Upvotes: -1

Related Questions