How to right-justify UILabel text?

Viewed 8411

How to right-justify UILabel text?

thanks

6 Answers

you can using this Extension Below for Swift 5 :

extension UILabel {
   func setJustifiedRight(_ title : String?) {
      if let desc = title {
           let text: NSMutableAttributedString = NSMutableAttributedString(string: desc)
           let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
           paragraphStyle.alignment = NSTextAlignment.justified
           paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
           paragraphStyle.baseWritingDirection = NSWritingDirection.rightToLeft
           text.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, text.length))
           self.attributedText = text
       }
    }
}

and simply set your text to your Label like This

self.YourLabel.setJustifiedRight("your texts")
Related