user123
user123

Reputation: 2721

Label Alignment in iOS 6 - UITextAlignment deprecated

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.

Upvotes: 194

Views: 90263

Answers (11)

majorl3oat
majorl3oat

Reputation: 4347

In iOS6 you can use

label.textAlignment = NSTextAlignmentCenter;

Upvotes: 400

Kstin
Kstin

Reputation: 679

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 
}

Upvotes: 0

Jerry Krinock
Jerry Krinock

Reputation: 5020

Swift 3:

label.textAlignment = NSTextAlignment.center

Upvotes: 0

Hiren Dhamecha
Hiren Dhamecha

Reputation: 648

in iOS 6 or greater

use this value :

self.lbl_age.textAlignment=NSTextAlignmentCenter;

Upvotes: 0

BrainyMonkey
BrainyMonkey

Reputation: 31

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

Upvotes: 0

Mubin Shaikh
Mubin Shaikh

Reputation: 384

labelName.textAlignment=NSTextAlignmentLeft;

Upvotes: 1

Gaurav Gilani
Gaurav Gilani

Reputation: 1584

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

Upvotes: 2

neoneye
neoneye

Reputation: 52171

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

Upvotes: 15

Aaron Brager
Aaron Brager

Reputation: 66244

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

Upvotes: 13

nhisyam
nhisyam

Reputation: 714

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

Upvotes: 19

Wienke
Wienke

Reputation: 3733

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

Upvotes: 44

Related Questions