Underline button text in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Underlined button text is a common way to present link-like actions such as “Forgot password” or “View terms.” In Swift, the implementation depends on whether you are using UIKit or SwiftUI, but the core idea is the same: apply underline styling to the button label rather than faking a button with a plain label. The most reliable solutions keep the text style reusable and handle interaction states cleanly.
UIKit: Use an Attributed Title
In UIKit, the standard solution is to assign an NSAttributedString to the button for the relevant control state. That gives you direct control over underline style, font, and color.
This is the most direct answer when the question is “how do I underline button text in Swift.”
Style Different Button States Explicitly
A button is not just one visual state. If you only style .normal, the underline may disappear or feel inconsistent when the button is highlighted, disabled, or selected. A small helper keeps that styling consistent.
This avoids duplicating inline attributed-string code across view controllers and makes future design changes easier.
SwiftUI: Underline the Text Inside the Button
SwiftUI is simpler because the label can just be a Text view with an underline modifier.
Using a custom label closure is usually better than the short Button("Title") form because it gives you precise control over styling.
Use AttributedString for Richer SwiftUI Labels
If the label needs more than a simple underline, modern SwiftUI can use AttributedString.
This becomes useful when only part of the text needs styling, or when the label is built from localized content with richer formatting rules.
Treat It as an Interactive Control, Not Decoration
An underline makes the text look like a link, but the element is still a button. That means normal UI control rules still apply:
- give it a tap target large enough to use comfortably
- preserve enough contrast between text and background
- keep the wording action-oriented
- make sure accessibility still communicates what the action does
If the button is important, underline alone is not enough. Color, spacing, and placement still matter.
Prefer Reusable Styling in Real Apps
If the same underlined style appears in several screens, centralize it instead of rebuilding the attributed string everywhere.
That approach keeps underline thickness, color choice, and interaction behavior consistent across the app.
Common Pitfalls
- Calling
setTitleaftersetAttributedTitle, which removes the underline styling you just applied. - Styling only the normal state and forgetting highlighted or disabled appearances.
- Using underline as the only cue even when color contrast or touch target size is weak.
- Repeating ad hoc attributed-string code in every screen instead of reusing a helper or factory.
- Assuming SwiftUI button styling automatically preserves custom underline behavior in every context.
Summary
- In UIKit, underline button text with
setAttributedTitleand anNSAttributedString. - In SwiftUI, underline the
Textinside the button label. - Treat interaction states explicitly so the style stays consistent when the button is pressed or disabled.
- Reuse styling helpers when the same link-style button appears across multiple screens.
- Underlined text should still behave like a proper accessible button, not just styled decoration.
Related reading
- Underline text in UIlabel
- Understand convertRecttoView, convertRectFromView, convertPointtoView and convertPointfromView methods
- Understanding colors on Android six characters
- Understanding dispatch_async
- Understanding Fragment's setRetainInstanceboolean
- Understanding NSRunLoop
- Undocumented NSURLErrorDomain error codes -1001, -1003 and -1004 using StoreKit
- Unexpected CFBundleExecutable key
.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.