Reputation: 37
I'm trying to set my UILabel
s textAlignment to .right
but it does not function at all. It's like it is not even there. Here is my code:
let productDescriptionLabel = UILabel(frame: CGRect(x: 10, y: imageViewHeight + productTitleLabel.frame.size.height + 10, width: viewWidth - 20, height: 0))
productDescriptionLabel.textColor = UIColor.darkGray
productDescriptionLabel.textAlignment = .right
productDescriptionLabel.text = productsDescriptionArray!
productDescriptionLabel.font = productDescriptionLabel.font.withSize(self.view.frame.height * self.relativeFontConstant)
productDescriptionLabel.numberOfLines = 0
let attrString = NSMutableAttributedString(string: productDescriptionLabel.text!)
let style = NSMutableParagraphStyle()
style.lineSpacing = 10
style.minimumLineHeight = 20
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: (productDescriptionLabel.text?.characters.count)!))
productDescriptionLabel.attributedText = attrString
productDescriptionLabel.sizeToFit()
productDescriptionLabel.lineBreakMode = .byWordWrapping
contentScrollView.addSubview(productDescriptionLabel)
All it shows is this
Upvotes: 1
Views: 212
Reputation: 31645
Since you are aiming to edit the label attributed string via a NSMutableParagraphStyle
instance (style
) , you should assign the desired alignment to your style
, by editing the its alignment
property:
style.alignment = .right
Also, because style
handles the alignment, there is no need to implement:
productDescriptionLabel.textAlignment = .right
Upvotes: 1