Label Alignment in iOS 6 - UITextAlignment deprecated
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Starting with iOS 6, UITextAlignment was deprecated in favor of NSTextAlignment. The functional idea did not change much. You still set the alignment on the label, but you now use the newer enum values that UIKit adopted for better consistency.
The Old and New API
Older Objective-C code often looked like this:
The modern replacement is:
The same change applies to left and right alignment:
- '
UITextAlignmentLeftbecomesNSTextAlignmentLeft' - '
UITextAlignmentCenterbecomesNSTextAlignmentCenter' - '
UITextAlignmentRightbecomesNSTextAlignmentRight'
So the fix is not architectural. It is a straightforward enum migration.
Swift Examples
In modern Swift, you usually write alignment like this:
Or explicitly:
Swift makes this cleaner because the property type already implies the enum.
Why Apple Made the Change
NSTextAlignment aligned UIKit text APIs more closely with the broader Cocoa and Foundation text model. It also added values such as .justified and .natural, which are useful for richer text layout and better handling of language direction.
That means the change was not just a rename. It also made the text-alignment API more expressive.
This matters in multilingual apps. A hard-coded left or right alignment is not always the most natural display choice once right-to-left languages enter the picture. The newer enum gave UIKit a better vocabulary for those cases.
Backward Compatibility Patterns
If you maintain very old code that still has to compile against legacy SDKs, you may see compatibility checks or conditional compilation. But for most modern maintenance work, the right answer is simply to replace the deprecated enum with NSTextAlignment.
A simple label setup in Objective-C now looks like this:
NSTextAlignmentNatural is particularly useful when you want the alignment to follow the natural reading direction of the current language.
Where Alignment Problems Really Come From
Developers sometimes change the enum and still think alignment is broken, when the real problem is layout. For example:
- the label frame is too narrow to show the effect clearly
- Auto Layout is resizing the label unexpectedly
- the label allows only one line and text is truncated
So if the new enum compiles but the UI still looks wrong, inspect the label's frame and constraints before blaming the alignment API.
Common Pitfalls
The most common mistake is replacing the enum name but overlooking related layout issues that make the alignment look incorrect.
Another mistake is using outdated examples copied from pre-iOS 6 code without translating them to NSTextAlignment.
A third pitfall is ignoring .natural and .justified, which can be better choices than hard-coded left or right alignment in some interfaces.
A fourth pitfall in mixed Objective-C and Swift codebases is updating new Swift files while leaving old Objective-C snippets untouched, which keeps deprecation warnings alive and makes the migration look incomplete.
Those small cleanup gaps are common in long-lived UIKit projects.
Summary
- '
UITextAlignmentwas deprecated and replaced byNSTextAlignment.' - The migration is usually a direct enum substitution.
- In Swift, alignment is commonly written as
label.textAlignment = .center. - '
NSTextAlignmentadds newer options such as.naturaland.justified.' - If alignment still looks wrong after the migration, inspect layout and label sizing rather than the enum itself.

