11

How to right-justify UILabel text?

thanks

Kristen Martinson
  • 1,829
  • 3
  • 22
  • 33

6 Answers6

7

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
Aryan
  • 2,675
  • 5
  • 24
  • 33
7
myLabel.textAlignment = UITextAlignmentRight;

iOS Developer Library is a good resource.

nekno
  • 19,177
  • 5
  • 42
  • 47
  • 15
    UITextAlignmentRight is deprecated since iOS6. Use NSTextAlignmentRight instead. – jbandi Nov 07 '13 at 00:38
  • `UITextAlignmentRight` or `NSTextAlignmentRight` only right align the text but the text is not justified see @Hashem Aboonajmi or @Aryan answers – Mohammad Alavi Jun 13 '17 at 18:39
5

The new method call is:

label.textAlignment = NSTextAlignmentRight;
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Laurent Rivard
  • 509
  • 4
  • 13
4

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")
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60
2

Here is: be careful if full justify needs to be applied, firstLineIndent should not be zero.

NSMutableParagraphStyle* paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;
paragraph.baseWritingDirection = NSWritingDirectionRightToLeft;
paragraph.firstLineHeadIndent = 1.0;
NSDictionary* attributes = @{
                             NSForegroundColorAttributeName: [UIColor colorWithRed:0.2 green:0.239 blue:0.451 alpha:1],NSParagraphStyleAttributeName: paragraph};
NSString* txt = @"your long text";
NSAttributedString* aString = [[NSAttributedString alloc] initWithString: txt attributes: attributes];
Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
0

it can be through interface builder. or label.textAlignment = UITextAlignmentRight;

Sachin Kumaram
  • 900
  • 1
  • 10
  • 27