NSAttributedString add text alignment
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Text alignment in an NSAttributedString is not applied as a standalone alignment flag on the string itself. It is applied through a paragraph style attribute. That detail matters because alignment belongs to paragraph formatting, not just to individual character styling such as font or color.
Use NSMutableParagraphStyle
To add alignment, create a paragraph style, set its alignment, and attach it to the attributed string with the .paragraphStyle key.
This is the core pattern. Without the paragraph style, there is no attributed-string alignment to apply.
Apply Alignment to a Mutable Attributed String
If you already have an attributed string and want to add alignment later, use NSMutableAttributedString.
This updates the full range. You can also apply the paragraph style only to a subset if you want different paragraphs to use different alignments.
Alignment Applies Per Paragraph
Because alignment is part of paragraph style, a single paragraph-style attribute affects paragraph layout. That becomes visible when the attributed string contains line breaks.
In real code, you usually calculate those ranges more carefully, but the important idea is that paragraph-level formatting can vary across the string.
Use It with UILabel, UITextView, or AppKit Controls
Once the attributed string carries the paragraph style, assign it to the UI control that renders attributed text.
If the string is already attributed, the paragraph style usually becomes the authoritative source for alignment behavior inside that attributed content.
This is one reason developers get confused when label.textAlignment does not seem to behave the way they expect after switching to attributedText. The text content now carries its own formatting information.
Common Alignment Values
NSMutableParagraphStyle supports the usual alignment modes:
- '
.left' - '
.right' - '
.center' - '
.justified' - '
.natural'
Example:
.natural is often useful when you want the system to pick the appropriate alignment direction for the current language context.
Keep Other Paragraph Settings Together
If you are already using paragraph style for alignment, it is often convenient to configure related paragraph settings in the same object.
This keeps layout-related text formatting in one place instead of scattering it across multiple unrelated mechanisms.
Common Pitfalls
The most common mistake is trying to add alignment without a paragraph style and expecting it to work like a simple font or color attribute. Another is applying the paragraph style to only part of a paragraph and then being surprised by inconsistent layout behavior. Developers also often set textAlignment on the label and forget that an attributed string with its own paragraph styles may define the effective alignment more explicitly. A final issue is reusing one mutable paragraph style object and then mutating it later, which can unintentionally affect multiple attributed strings if you are not careful about copying behavior.
Summary
- Text alignment in an
NSAttributedStringis applied through.paragraphStyle. - Use
NSMutableParagraphStyleand set itsalignmentproperty. - Alignment is paragraph formatting, not just character formatting.
- Apply the paragraph style to the desired attributed-string range.
- When using attributed text in UI controls, paragraph styles often define the effective alignment behavior.
Related reading
- NSAttributedString background color and rounded corners
- NSCameraUsageDescription in iOS 10.0 runtime crash?
- NSDate - Convert Date to GMT
- NSDate beginning of day and end of day
- NSDate Comparison using Swift
- NSDefaultRunLoopMode vs NSRunLoopCommonModes
- NSDefaultRunLoopMode vs NSRunLoopCommonModes
- NSDictionary with ordered keys
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.