Center text in a UILabel
Besides using code, as Henrik suggested, you can also set the appropriate property in Interface Builder.
This is depreciated now in iOS6.
You should use:
yourLabel.textAlignment = NSTextAlignmentCenter;
The Code is
[yourLabel setTextAlignment:UITextAlignmentCenter];
or (of course) the Obj-C 2.0 Dot-Syntax
yourLabel.textAlignment = UITextAlignmentCenter;
For iOS 6 (and higher) you should use NSTextAlignmentCenter
instead of UITextAlignmentCenter
:
yourLabel.textAlignment = NSTextAlignmentCenter;
Source
And if you want backward compatibiliy to iOS 5 also you can do this,
#ifdef __IPHONE_6_0
# define ALIGN_CENTER NSTextAlignmentCenter
#else
# define ALIGN_CENTER UITextAlignmentCenter
#endif
Swift 3
yourLabel.textAlignment = .center