How do you programmatically center the alignment of a text label for iOS?
How do you programmatically center the alignment of a text label for iOS?
I want to set the alignment of a text label, how can I do that?
8 Answers
8
I think there are the answers who helped you out. The correct way to do this is:
yourLabelName.textAlignment = NSTextAlignmentCenter;
for more documentation you can read this:
https://developer.apple.com/documentation/uikit/uilabel
Here you are,
yourLabel.textAlignment = UITextAlignmentCenter
EDIT
if you target above iOS6 use NSTextAlignmentCenter
as UITextAlignmentCenter
is depreciated
NSTextAlignmentCenter
UITextAlignmentCenter
Hope it helps.
This has changed as of iOS 6.0, UITextAlignment has been deprecated. The correct way to do this now is:
yourLabel.textAlignment = NSTextAlignmentCenter;
Here is the NSTextAlignment enumerable that gives the options for text alignment:
Objective-C:
enum {
NSTextAlignmentLeft = 0,
NSTextAlignmentCenter = 1,
NSTextAlignmentRight = 2,
NSTextAlignmentJustified = 3,
NSTextAlignmentNatural = 4,
};
typedef NSInteger NSTextAlignment;
Swift:
enum NSTextAlignment : Int {
case Left
case Center
case Right
case Justified
case Natural
}
Source
@nari please accept this answer. Is the most up to date
– mm24
Jan 28 '15 at 11:47
label.textAlignment = NSTextAlignmentCenter;
see UILabel documentation
depricated in ios 6
– khatz0406
Nov 19 '13 at 10:40
If you have a multiline UILabel you should use a NSMutableParagraphStyle
yourLabelName.numberOfLines = 0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
let attributes : [String : AnyObject] = [NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 15)!, NSParagraphStyleAttributeName: paragraphStyle]
let attributedText = NSAttributedString.init(string: subTitleText, attributes: attributes)
yourLabelName.attributedText = attributedText
In Swift 3 and beyond it should be
yourlabel.textAlignment = NSTextAlignment.center
in swift 4:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
// in Swift 4.2
let attributedString = NSMutableAttributedString(string: "Your String", attributes:[NSAttributedString.Key.paragraphStyle:paragraphStyle])
// in Swift 4.1--
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])
let yourLabel = UILabel()
yourLabel.attributedText = attributedString
Yourlabel.textAlignment = UITextAlignmentCenter;
this answer is already posted
– Arun Manjhi
Oct 31 '13 at 13:24
UITextAlignmentCenter
is deprecated. Use NSTextAlignmentCenter
as suggested below instead.– Nick Merrill
Jan 12 '14 at 1:42
UITextAlignmentCenter
NSTextAlignmentCenter
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How about for the label in a tableview row? This is not working
– Dani Springer
Jul 5 at 21:11