UILabel Align Text to center
Asked Answered
K

10

245

How do I align text in UILabel?

Katzman answered 19/4, 2011 at 21:7 Comment(0)
S
580

From iOS 6 and later UITextAlignment is deprecated. use NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Swift Version from iOS 6 and later

myLabel.textAlignment = .center
Souter answered 12/7, 2012 at 8:20 Comment(2)
swift version could be simplified :) myLabel.textAlignment = .CenterElvaelvah
.center ← lowercaseWigeon
B
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

Boulogne answered 19/4, 2011 at 21:14 Comment(2)
UITextAlignment is deprecated since iOS 5. Use NSTextAlignment instead.Pythoness
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); Speakeasy
K
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
Knavish answered 24/8, 2014 at 4:34 Comment(0)
C
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.

Chitwood answered 19/4, 2011 at 21:13 Comment(0)
G
6

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

Gaslit answered 11/12, 2013 at 9:24 Comment(0)
P
5

For Swift 3 it's:

label.textAlignment = .center
Pryce answered 19/12, 2016 at 9:27 Comment(0)
E
4

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

Eckardt answered 19/4, 2013 at 7:25 Comment(0)
S
3
Label.textAlignment = NSTextAlignmentCenter;
Sepalous answered 23/4, 2014 at 9:30 Comment(0)
K
3

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

title.TextAlignment = UITextAlignment.Center;
Knickers answered 16/12, 2016 at 7:9 Comment(1)
The question is not about Xamarin. And UITextAlignment is deprecated in iOS 6!Holbrooke
S
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)
Sudoriferous answered 12/10, 2018 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.