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:
I don't know what else to use that NSTextAlignment.Justified
to justified my text. Should I use a UITextView
instead?
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:
Credits for NSBaselineOffsetAttributedName: https://stackoverflow.com/a/19445666/2494219