UILabel Align Text to center

IosCocoa TouchTextAlignmentUilabel

Ios Problem Overview


How do I align text in UILabel?

Ios Solutions


Solution 1 - Ios

From iOS 6 and later UITextAlignment is deprecated. use NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Swift Version from iOS 6 and later

myLabel.textAlignment = .center

Solution 2 - Ios

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

Solution 3 - Ios

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

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

For Swift 3 it's:

label.textAlignment = .center

Solution 7 - Ios

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

Solution 8 - Ios

Label.textAlignment = NSTextAlignmentCenter;

Solution 9 - Ios

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

title.TextAlignment = UITextAlignment.Center;

Solution 10 - Ios

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvivianaranhaView Question on Stackoverflow
Solution 1 - IosAravindhanView Answer on Stackoverflow
Solution 2 - IosVishal ShahView Answer on Stackoverflow
Solution 3 - IosAdamTView Answer on Stackoverflow
Solution 4 - IosJohn ParkerView Answer on Stackoverflow
Solution 5 - IosJayprakash DubeyView Answer on Stackoverflow
Solution 6 - IosMichał KwiecieńView Answer on Stackoverflow
Solution 7 - IosRinju JainView Answer on Stackoverflow
Solution 8 - IosMubin ShaikhView Answer on Stackoverflow
Solution 9 - IosDilip JangidView Answer on Stackoverflow
Solution 10 - IosNareshView Answer on Stackoverflow