Swift
iOS Development
Button Title Alignment
SwiftUI
UIButton

How to change Button Title Alignment in Swift?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

Changing a button title's alignment in Swift depends on which UI system you are using and what you actually mean by "alignment." Sometimes you want the text inside the button to be leading or trailing. In other cases, you want the whole button content to move. UIKit and SwiftUI both support this, but the correct property is not always the same.

UIKit: move the button content first

For UIButton, the most important property is often contentHorizontalAlignment. This controls how the button's content sits inside the button bounds.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let button = UIButton(type: .system)
8        button.frame = CGRect(x: 20, y: 120, width: 240, height: 50)
9        button.setTitle("Continue", for: .normal)
10        button.backgroundColor = .systemBlue
11        button.setTitleColor(.white, for: .normal)
12
13        button.contentHorizontalAlignment = .leading
14        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
15
16        view.addSubview(button)
17    }
18}

This places the content near the leading edge with some padding. If your goal is a left-aligned title in a standard button, this is usually the property that produces the visible result.

When titleLabel?.textAlignment matters

titleLabel?.textAlignment affects the text inside the title label itself. That matters most when the title spans multiple lines or the title label fills more than the minimal text width.

swift
1button.titleLabel?.numberOfLines = 2
2button.titleLabel?.textAlignment = .left
3button.setTitle("Download the full report now", for: .normal)
4button.contentHorizontalAlignment = .leading

If you set only textAlignment and do not change contentHorizontalAlignment, the title may still appear centered because the label itself remains centered within the button. That is why many "it did nothing" reports happen. The label alignment and the content alignment solve related but different layout problems.

Modern UIKit with UIButton.Configuration

On newer iOS versions, configuration-based buttons are common. In that world, title alignment can be expressed directly through the configuration object.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        var config = UIButton.Configuration.filled()
8        config.title = "Save changes"
9        config.titleAlignment = .leading
10        config.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 16, bottom: 10, trailing: 16)
11
12        let button = UIButton(configuration: config)
13        button.frame = CGRect(x: 20, y: 120, width: 260, height: 52)
14
15        view.addSubview(button)
16    }
17}

If you are using UIButton.Configuration, prefer configuring the button through that API instead of mixing many older properties. The configuration system is designed to own more of the layout behavior.

SwiftUI approach

In SwiftUI, you do not usually align the title by mutating a UIButton. Instead, you align the label view inside a frame.

swift
1import SwiftUI
2
3struct ContentView: View {
4    var body: some View {
5        Button(action: {}) {
6            Text("Continue")
7                .frame(maxWidth: .infinity, alignment: .leading)
8                .padding(.horizontal, 16)
9        }
10        .buttonStyle(.borderedProminent)
11        .padding()
12    }
13}

The idea is similar to UIKit: align the content container, not just the text object. SwiftUI makes that explicit through view layout modifiers.

Choosing the right property

A good rule is:

  • If the whole button content should move left or right, use content alignment.
  • If multi-line text inside the label should align differently, use text alignment.
  • If the button uses UIButton.Configuration, prefer configuration-based alignment.
  • If you are in SwiftUI, align the label view with a frame modifier.

Thinking this way prevents you from changing a text property when the real issue is the button's content container.

Common Pitfalls

The most common mistake is setting titleLabel?.textAlignment alone and expecting the whole title to move. That only changes how text is drawn inside the label, not where the content block sits in the button.

Another issue is mixing old-style button properties with configuration-based buttons. When a UIButton.Configuration is active, some older layout adjustments can be overridden or behave differently than expected.

Multi-line titles also surprise people. A single-line title may look centered even when text alignment changes because the label shrinks to the text width. Add width, line wrapping, or content alignment if you need the visual change to be obvious.

Finally, remember right-to-left languages. Using .leading and .trailing is usually safer than hard-coding left and right because it respects localization.

Summary

  • Use contentHorizontalAlignment to move the button content inside a UIButton.
  • Use titleLabel?.textAlignment for how multi-line text is drawn inside the label.
  • Prefer UIButton.Configuration properties for modern configuration-based buttons.
  • In SwiftUI, align the label with layout modifiers such as frame.
  • Prefer leading and trailing semantics over hard-coded left and right.

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.