Label Alignment in iOS 6 - UITextAlignment deprecated

IosAlignmentLabel

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

In iOS6 you can use

label.textAlignment = NSTextAlignmentCenter;

Solution 2 - Ios

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.)

Solution 3 - Ios

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

Solution 4 - Ios

#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];

Solution 5 - Ios

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

Solution 6 - Ios

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];

Solution 7 - Ios

labelName.textAlignment=NSTextAlignmentLeft;

Solution 8 - Ios

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

Solution 9 - Ios

in iOS 6 or greater

use this value :

self.lbl_age.textAlignment=NSTextAlignmentCenter;

Solution 10 - Ios

Swift 3:

label.textAlignment = NSTextAlignment.center

Solution 11 - Ios

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 
}

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
Questionuser123View Question on Stackoverflow
Solution 1 - Iosmajorl3oatView Answer on Stackoverflow
Solution 2 - IosWienkeView Answer on Stackoverflow
Solution 3 - IosnhisyamView Answer on Stackoverflow
Solution 4 - IosneoneyeView Answer on Stackoverflow
Solution 5 - IosAaron BragerView Answer on Stackoverflow
Solution 6 - IosGaurav GilaniView Answer on Stackoverflow
Solution 7 - IosMubin ShaikhView Answer on Stackoverflow
Solution 8 - IosBrainyMonkeyView Answer on Stackoverflow
Solution 9 - IosHiren DhamechaView Answer on Stackoverflow
Solution 10 - IosJerry KrinockView Answer on Stackoverflow
Solution 11 - IosKstinView Answer on Stackoverflow