193

Seems like UITextAlignmentCenter is deprecated in iOS 6.

I still use it and works well, but it gives a warning.

How can I fix this?

label.textAlignment = UITextAlignmentCenter;

Thanks.

Nayan
  • 3,014
  • 2
  • 17
  • 33
user123
  • 2,711
  • 5
  • 21
  • 25

11 Answers11

398

In iOS6 you can use

label.textAlignment = NSTextAlignmentCenter;
Halo
  • 1,730
  • 1
  • 8
  • 31
majorl3oat
  • 4,327
  • 1
  • 18
  • 21
  • It's possible that the enum MAY change at some time in the future and cause unexpected results. That's all. No doomsday scenario in this case, but better practices should prevail. – Brenden Nov 02 '12 at 19:36
  • 17
    majorl3oat: It is NOT best practice to use a constant number instead of enum. The enum helps with the readability of the code which is an extremely important factor. That it might break in the future is just bad design from Apple, and if they break it I would rather have compilation errors than risking that the alignment gets wrong without me noticing. – Robin Andersson Nov 14 '12 at 12:45
  • 6
    Robin is COMPLETELY correct. Using an enum IS best practice, no doubt. You can ALWAYS assume that NSTextAlignmentCenter means CENTER, you can in NO WAY assume that 1 always means CENTER. Unless Apple is completely daft. – Rasmus Nov 14 '12 at 13:17
  • an enum is specifically so the code doesn't break in the future. If they decide that the 1 should be a 42, the enum will continue to work, the hard-coded value will fail. – Bryan Oakley Dec 03 '12 at 16:09
  • Yes please remove the textAlignment = 1, it will confuse people and spread bad knowledge – MobileMon Apr 05 '13 at 14:53
  • @Rasmus — Welp, Apple is pretty daft, it would seem. Have a look inside `NSText.h` of `UIKit`. There, 1 is defined to be Center if the target OS is iPhone, otherwise it is defined to be Right. All the more reason to use `NSTextAlignmentCenter`. – Todd Lehman Apr 09 '16 at 01:50
44

The labelAlignment property change is probably related to Apple’s introducing NSAttributedStrings to more of the iOS controls, and therefore needing to change the UIText… properties to NSText… properties.

So if you’ve upgraded to iOS6, you’re in clover; just switch from UITextAlignmentCenter to NSTextAlignmentCenter and enjoy the fancy new strings.

But if you’re working with a complex project and would prefer that the earth not move so much under your feet, you might want to stick with an older version for a while, and adapt your code for multiple versions, something like this:

// This won't compile:
if ([label respondsToSelector:@selector(attributedText:)]) 
    label.textAlignment = UITextAlignmentCenter;
else 
    label.textAlignment = NSTextAlignmentCenter;

The above approach works for new methods; you get warnings but everything runs fine. But when the compiler sees a constant that it doesn’t know about, it turns red and stops in its tracks. There’s no way to sneak NSTextAlignmentCenter past it. (Well, there might be a way to customize the compiler’s behavior here, but it seems inadvisable.)

The workaround is to add some conditional preprocessor defines. If you put something like this in your class’s h file (or perhaps in an imported constants file -- which must itself include #import <UIKit/UIKit.h> in order to ever know about the NSText... constants)…

#ifdef NSTextAlignmentCenter // iOS6 and later
#   define kLabelAlignmentCenter    NSTextAlignmentCenter
#   define kLabelAlignmentLeft      NSTextAlignmentLeft
#   define kLabelAlignmentRight     NSTextAlignmentRight
#   define kLabelTruncationTail     NSLineBreakByTruncatingTail 
#   define kLabelTruncationMiddle   NSLineBreakByTruncatingMiddle
#else // older versions
#   define kLabelAlignmentCenter    UITextAlignmentCenter
#   define kLabelAlignmentLeft      UITextAlignmentLeft
#   define kLabelAlignmentRight     UITextAlignmentRight
#   define kLabelTruncationTail     UILineBreakModeTailTruncation
#   define kLabelTruncationMiddle   UILineBreakModeMiddleTruncation
#endif

…you can do this:

label.textAlignment = kLabelAlignmentCenter;

And this:

label.lineBreakMode = kLabelTruncationMiddle;

Etc.

Since these UIText/NSText changes are likely to be popping up for multiple controls, this approach is quite handy.

(Caveat: Being a member of the aforementioned steady-earth lovers, I have tested this with an old version, but not yet with iOS6.)

Wienke
  • 3,723
  • 27
  • 40
  • As of Xcode 9 iOS 11.4 SDK , this is still the solution for me. [labelOne setFont:[UIFont fontWithName:@"HelveticaNeue" size:20]]; labelOne.textAlignment = NSTextAlignmentLeft; //Taken out - kCTLeftTextAlignment; labelOne.text = dateStr; [headerView addSubview:labelOne]; – Matthew Ferguson Jul 21 '18 at 17:20
19

NSTextAlignmentCenter can be used in place of UITextAlignmentCenter and a list of other replacements is below:

#ifdef __IPHONE_6_0 // iOS6 and later
#   define UITextAlignmentCenter    NSTextAlignmentCenter
#   define UITextAlignmentLeft      NSTextAlignmentLeft
#   define UITextAlignmentRight     NSTextAlignmentRight
#   define UILineBreakModeTailTruncation     NSLineBreakByTruncatingTail
#   define UILineBreakModeMiddleTruncation   NSLineBreakByTruncatingMiddle
#endif
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
nhisyam
  • 714
  • 7
  • 11
15
#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0)
# define ALIGN_CENTER NSTextAlignmentCenter
#else
# define ALIGN_CENTER UITextAlignmentCenter
#endif

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
label.text = @"There is no spoon";
label.textAlignment = ALIGN_CENTER;
[self addSubview:label];
neoneye
  • 50,398
  • 25
  • 166
  • 151
  • A better construction would be to use something more like: #ifdef (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0) .... Otherwise you are going to be getting UITextAlignmentCenter in iOS 7. – Ray Fix Feb 16 '14 at 14:09
13

You don't have to do either of these. Xcode 4.5 will compile the NSTextAlignmentCenter, etc. fine in iOS 5.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • 1
    This seems to be true. I have the issue with UILineBreakMode and UITextAlignment-- and replacing them with NSLineBreakMode and NSTextAlignment-- works fine in Xcode 4.6.3 in the 5.0 simulator and on an iPad running 5.-. – JScarry Jun 23 '13 at 01:55
2
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 40)];
[label1 setText:@"Your String"];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 sizeToFit];

//For Center Alignment
[label1 setTextAlignment:NSTextAlignmentCenter];

//For Right Alignment
[label1 setTextAlignment:NSTextAlignmentRight];

//For Left Alignment
[label1 setTextAlignment:NSTextAlignmentLeft];

// Add the label into the view

[self.view addSubview:label1];
Gaurav Gilani
  • 1,584
  • 14
  • 18
1
labelName.textAlignment=NSTextAlignmentLeft;
Kampai
  • 22,848
  • 21
  • 95
  • 95
Mubin Shaikh
  • 374
  • 4
  • 9
0

I had a similar issue and used the following: detailsLabel.textAlignment = NSTextAlignmentCenter;

0

in iOS 6 or greater

use this value :

self.lbl_age.textAlignment=NSTextAlignmentCenter;

Hiren Dhamecha
  • 658
  • 5
  • 15
0

Swift 3:

label.textAlignment = NSTextAlignment.center
Jerry Krinock
  • 4,860
  • 33
  • 39
0

For Swift 5+ use:

yourLabel.textAlignment = .center

where You can set:

public enum NSTextAlignment : Int {
    case left = 0 // Visually left aligned
    case center = 1 // Visually centered
    case right = 2 // Visually right aligned
    case justified = 3 // Fully-justified. The last line in a paragraph is natural-aligned.
    case natural = 4 // Indicates the default alignment for script 
}
Kstin
  • 659
  • 1
  • 4
  • 18