How to right-justify UILabel text?

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

How to right-justify UILabel text?

thanks

6条回答
  •  独厮守ぢ
    2021-02-14 12:59

    You should set text alignment to justified and set attributed string base writing direction to RightToLeft:

    var label: UILabel = ...
    var text: NSMutableAttributedString = ...
    var paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
    paragraphStyle.alignment = NSTextAlignment.Justified
    paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
    paragraphStyle.baseWritingDirection = NSWritingDirection.RightToLeft
    text.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, text.length))
    label.attributedText = text
    

提交回复
热议问题