245

How do I align text in UILabel?

Naresh
  • 16,698
  • 6
  • 112
  • 113
vivianaranha
  • 2,781
  • 6
  • 33
  • 47

10 Answers10

579

From iOS 6 and later UITextAlignment is deprecated. use NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Swift Version from iOS 6 and later

myLabel.textAlignment = .center
Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
83

Here is a sample code showing how to align text using UILabel:

label = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 12)];
label.textAlignment = NSTextAlignmentCenter;

You can read more about it here UILabel

Rajat
  • 10,977
  • 3
  • 38
  • 55
Vishal Shah
  • 1,042
  • 8
  • 11
  • 27
    `UITextAlignment` is deprecated since iOS 5. Use `NSTextAlignment` instead. – Philip007 Nov 07 '12 at 15:43
  • False, UITextAligment is deprecated. In UIStringDrawing.h (UIKit) you can find this code: `// Deprecated: use NSTextAlignment enum in UIKit/NSText.h typedef NS_ENUM(NSInteger, UITextAlignment) { UITextAlignmentLeft = 0, UITextAlignmentCenter, UITextAlignmentRight, // could add justified in future } NS_DEPRECATED_IOS(2_0,6_0); ` – aramusss Jun 10 '15 at 11:36
13

To center text in a UILabel in Swift (which is targeted for iOS 7+) you can do:

myUILabel.textAlignment = .Center

Or

myUILabel.textAlignment = NSTextAlignment.Center
AdamT
  • 6,405
  • 10
  • 49
  • 75
8

N.B.: As per the UILabel class reference, as of iOS 6 this approach is now deprecated.

Simply use the textAlignment property to see the required alignment using one of the UITextAlignment values. (UITextAlignmentLeft, UITextAlignmentCenter or UITextAlignmentRight.)

e.g.: [myUILabel setTextAlignment:UITextAlignmentCenter];

See the UILabel Class Reference for more information.

John Parker
  • 54,048
  • 11
  • 129
  • 129
6

Use yourLabel.textAlignment = NSTextAlignmentCenter; for iOS >= 6.0 and yourLabel.textAlignment = UITextAlignmentCenter; for iOS < 6.0.

Intrications
  • 16,782
  • 9
  • 50
  • 50
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
5

For Swift 3 it's:

label.textAlignment = .center
Michał Kwiecień
  • 2,734
  • 1
  • 20
  • 23
4

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

Rinju Jain
  • 1,694
  • 1
  • 14
  • 23
3
Label.textAlignment = NSTextAlignmentCenter;
codercat
  • 22,873
  • 9
  • 61
  • 85
Mubin Shaikh
  • 374
  • 4
  • 9
3

In xamarin ios suppose your label name is title then do the following

title.TextAlignment = UITextAlignment.Center;
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Dilip Jangid
  • 754
  • 1
  • 10
  • 20
  • 2
    The question is not about Xamarin. And `UITextAlignment` is [deprecated in iOS 6](https://developer.apple.com/reference/uikit/uitextalignment?language=objc)! – FelixSFD Dec 16 '16 at 07:12
2

In Swift 4.2 and Xcode 10

let lbl = UILabel(frame: CGRect(x: 10, y: 50, width: 230, height: 21))
lbl.textAlignment = .center //For center alignment
lbl.text = "This is my label fdsjhfg sjdg dfgdfgdfjgdjfhg jdfjgdfgdf end..."
lbl.textColor = .white
lbl.backgroundColor = .lightGray//If required
lbl.font = UIFont.systemFont(ofSize: 17)

 //To display multiple lines in label
lbl.numberOfLines = 0
lbl.lineBreakMode = .byWordWrapping

lbl.sizeToFit()//If required
yourView.addSubview(lbl)
Naresh
  • 16,698
  • 6
  • 112
  • 113