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.
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.
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.
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.
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.
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
contentHorizontalAlignmentto move the button content inside aUIButton. - Use
titleLabel?.textAlignmentfor how multi-line text is drawn inside the label. - Prefer
UIButton.Configurationproperties 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
- How to change color of Android ListView separator line?
- how to change color of textview hyperlink?
- How to change color of the back arrow in the new material theme?
- How to change color of the back arrow in the new material theme?
- How to change colors of a Drawable in Android?
- How to change display name of an app in react-native
- How to change font of UIButton with Swift
- How to change font of UIButton with Swift
.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.