How to right-justify UILabel text?

前端 未结 6 1153
悲哀的现实
悲哀的现实 2021-02-14 12:38

How to right-justify UILabel text?

thanks

6条回答
  •  春和景丽
    2021-02-14 13:05

    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")
    

提交回复
热议问题