UILabel
Swift
Line Spacing
iOS Development
SwiftUI

How to Increase Line spacing in UILabel in Swift

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

UILabel does not expose a direct lineSpacing property for plain text. To increase line spacing, you need NSAttributedString with a configured NSMutableParagraphStyle. This is straightforward, but many apps apply it inconsistently and end up with clipped text, broken dynamic type behavior, or styles lost when label text updates.

A robust solution should include paragraph style setup, label configuration (numberOfLines, wrapping), and reusable helpers so line spacing stays consistent across screens. This article covers practical Swift patterns for production iOS apps.

Core Sections

1. Apply line spacing using attributed text

swift
1import UIKit
2
3func applyLineSpacing(
4    to label: UILabel,
5    text: String,
6    spacing: CGFloat,
7    alignment: NSTextAlignment = .natural
8) {
9    let paragraph = NSMutableParagraphStyle()
10    paragraph.lineSpacing = spacing
11    paragraph.alignment = alignment
12
13    let attributed = NSAttributedString(
14        string: text,
15        attributes: [
16            .paragraphStyle: paragraph,
17            .font: label.font as Any,
18            .foregroundColor: label.textColor as Any
19        ]
20    )
21
22    label.attributedText = attributed
23}

lineSpacing affects distance between baselines for multi-line text.

2. Configure label layout correctly

swift
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping

If numberOfLines is 1, line spacing has no visible effect. Auto Layout constraints must also allow vertical growth.

3. Keep style when text changes

Setting label.text = ... later removes attributed styling. Reapply attributed text in one place.

swift
func updateDescription(_ text: String) {
    applyLineSpacing(to: descriptionLabel, text: text, spacing: 6)
}

Centralizing this avoids regressions during UI updates.

4. Support dynamic type

When fonts scale, line spacing should usually scale too.

swift
let baseSpacing: CGFloat = 4
let scaled = UIFontMetrics.default.scaledValue(for: baseSpacing)
applyLineSpacing(to: label, text: content, spacing: scaled)

Pair with adjustsFontForContentSizeCategory = true for accessibility.

5. Reusable UILabel extension

swift
1extension UILabel {
2    func set(text: String, lineSpacing: CGFloat, alignment: NSTextAlignment = .natural) {
3        let paragraph = NSMutableParagraphStyle()
4        paragraph.lineSpacing = lineSpacing
5        paragraph.alignment = alignment
6
7        attributedText = NSAttributedString(
8            string: text,
9            attributes: [
10                .paragraphStyle: paragraph,
11                .font: font as Any,
12                .foregroundColor: textColor as Any
13            ]
14        )
15    }
16}

Extensions improve consistency and reduce copy-paste styling code.

6. Interface Builder integration strategy

Line spacing cannot be configured directly in IB for plain UILabel text. Apply styling in awakeFromNib or view configuration methods.

swift
1override func awakeFromNib() {
2    super.awakeFromNib()
3    titleLabel.set(text: titleLabel.text ?? "", lineSpacing: 5)
4}

Do this after localization strings are assigned.

7. Measure rendering for complex text

For long text blocks, verify height calculations use attributed content.

swift
let size = label.sizeThatFits(CGSize(width: 280, height: .greatestFiniteMagnitude))

Plain-string height assumptions can under-measure attributed text and cause clipping.

Common Pitfalls

  • Setting lineSpacing but leaving numberOfLines = 1, making changes invisible.
  • Overwriting attributedText by assigning label.text later in the lifecycle.
  • Forgetting to include font/color attributes, causing style resets or inconsistent appearance.
  • Applying fixed spacing that does not scale with dynamic type accessibility settings.
  • Calculating label height as if text were plain, resulting in truncation for attributed strings.

Summary

To increase line spacing in UILabel, use an attributed string with NSMutableParagraphStyle.lineSpacing and ensure the label is configured for multi-line rendering. Reapply styling whenever text changes, scale spacing for dynamic type, and prefer reusable helper APIs to keep behavior consistent across the app. With these patterns, typography remains readable, accessible, and stable under real UI updates.

A practical way to harden this topic in real projects is to add a small operational checklist and treat it as part of your engineering standard, not a one-off fix. Start by creating one minimal failing case and one passing case that represent real input from production logs. Then automate those checks in CI so regressions are caught before release. Add lightweight instrumentation around the critical branch where this logic runs, and include structured fields that let you filter by version, environment, and error type. This gives you fast feedback when behavior changes after dependency upgrades or refactors.

For long-term maintainability on how to increase line spacing in uilabel in swift 1, keep one source of truth for helper logic instead of duplicating variants across services or UI layers. Document assumptions near the code, including data format, edge-case behavior, and expected fallback policy. During code review, verify that example inputs and tests cover empty values, malformed values, and high-volume scenarios. Teams that combine explicit assumptions, repeatable tests, and basic observability typically avoid the same category of bug recurring every quarter.


Course illustration
Course illustration

All Rights Reserved.