NSAttributedString
text alignment
iOS development
Swift programming
attributed strings

NSAttributedString add text alignment

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

swift
1import UIKit
2
3let style = NSMutableParagraphStyle()
4style.alignment = .center
5
6let attributes: [NSAttributedString.Key: Any] = [
7    .paragraphStyle: style,
8    .font: UIFont.systemFont(ofSize: 18)
9]
10
11let text = NSAttributedString(string: "Centered text", attributes: attributes)

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.

swift
1import UIKit
2
3let mutable = NSMutableAttributedString(string: "Hello\nWorld")
4let style = NSMutableParagraphStyle()
5style.alignment = .right
6
7mutable.addAttribute(
8    .paragraphStyle,
9    value: style,
10    range: NSRange(location: 0, length: mutable.length)
11)

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.

swift
1import UIKit
2
3let text = "Left paragraph\nCentered paragraph"
4let mutable = NSMutableAttributedString(string: text)
5
6let leftStyle = NSMutableParagraphStyle()
7leftStyle.alignment = .left
8
9let centerStyle = NSMutableParagraphStyle()
10centerStyle.alignment = .center
11
12mutable.addAttribute(.paragraphStyle, value: leftStyle, range: NSRange(location: 0, length: 14))
13mutable.addAttribute(.paragraphStyle, value: centerStyle, range: NSRange(location: 15, length: 18))

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.

swift
1import UIKit
2
3let label = UILabel()
4label.numberOfLines = 0
5label.attributedText = 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:

swift
style.alignment = .justified

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

swift
1let style = NSMutableParagraphStyle()
2style.alignment = .center
3style.lineSpacing = 6
4style.paragraphSpacing = 12

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 NSAttributedString is applied through .paragraphStyle.
  • Use NSMutableParagraphStyle and set its alignment property.
  • 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.