UITextAlignmentCenter
seems to be deprecated in iOS 6. What are my alternatives?
Asked
Active
Viewed 3.2k times
53

Michael Petrotta
- 59,888
- 27
- 145
- 179

IOS_Dev
- 655
- 2
- 6
- 7
5 Answers
103
For IOS 6 you should use NSTextAlignmentCenter
instead of UITextAlignmentCenter
:
button.titleLabel.textAlignment = NSTextAlignmentCenter;
And if you want backward compatibiliy to IOS 5 also you can do this,
#ifdef __IPHONE_6_0
# define ALIGN_CENTER NSTextAlignmentCenter
#else
# define ALIGN_CENTER UITextAlignmentCenter
#endif
9
#ifdef __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];

Arash Zeinoddini
- 801
- 13
- 19
-
Nice and elegant solution. Thanks for sharing =) – Mark Mooibroek Apr 17 '13 at 07:56
7
You can do
newLabel.textAlignment = NSTextAlignmentCenter;
instead of
newLabel.textAlignment = UITextAlignmentCenter;
hope it helps.

iEinstein
- 2,100
- 1
- 21
- 32
4
You should use NSTextAlignmentCenter
instead according to Apple's documentation.
You should also use NSTextAlignmentLeft
and NSTextAlignmentRight
instead of UITextAlignmentLeft
and UITextAlignmentRight
, respectively.

michaellindahl
- 2,012
- 5
- 36
- 64

zachjs
- 1,738
- 1
- 11
- 22