How can I make a clickable link in an NSAttributedString?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
An NSAttributedString can store link metadata, but the string alone does not become interactive just because it contains a .link attribute. To make a link actually tappable on iOS, you need both the attributed string and a view that knows how to interpret link attributes, most commonly UITextView.
That is the part many people miss. NSAttributedString describes the content, but the hosting view decides whether the user can interact with it.
Add the .link Attribute
The first step is to mark the desired range with a link:
At this point, the attributed string contains link information, but it is still only data. Nothing is clickable yet until a supporting view renders it.
Use UITextView for Interactive Links
UILabel can display attributed text, but it does not natively handle clickable links. UITextView does:
With this setup, the linked text is interactive without needing your own tap-location hit testing.
Intercept or Customize Link Behavior
If you want to open some links internally or prevent automatic navigation, use the text view delegate:
This lets you treat some links as external URLs and others as app-specific routes.
What If You Must Use UILabel?
If the design requires UILabel, clickable links become a manual problem. You need to:
- display the attributed text
- attach a gesture recognizer
- map the tap location to a character range
- inspect whether that range has a
.linkattribute
That is possible, but it is more code and easier to get wrong. If the main requirement is interactive rich text, UITextView is usually the simpler and more accessible control.
Styling Links Consistently
If you want one consistent link appearance across the whole text view, set linkTextAttributes:
That is often cleaner than repeating the same color and underline attributes in every linked range.
Common Pitfalls
The biggest mistake is adding a .link attribute and then displaying the attributed string in UILabel, expecting the link to become tappable automatically. It will not.
Another common issue is forgetting to set isEditable = false on UITextView, which makes the control behave like an editor instead of a simple read-only text surface.
Developers also sometimes style text to look like a link without actually adding the .link attribute. Blue underlined text is not interactive unless the attribute and the hosting view both support it.
Finally, if you implement custom label hit testing, be careful about accessibility. UITextView already handles many details that custom gesture logic has to recreate manually.
Summary
- A clickable link needs both a
.linkattribute and a view that understands link interaction. - '
UITextViewis the simplest UIKit control for tappable attributed links.' - Use the text view delegate when you need to intercept or customize link taps.
- '
UILabelcan show linked styling, but clickable behavior there is manual work.' - Keep styling and interaction separate: appearance alone does not make a link interactive.
Related reading
- How can I make a function complete before calling others in an IBAction?
- How can I make a function execute every second in swift?
- How can I make a UITextField move up when the keyboard is present - on starting to edit?
- How can I make a weak protocol reference in 'pure' Swift without objc
- How can I make my custom objects Parcelable?
- How can I make the memberwise initialiser public, by default, for structs in Swift?
- How can I mask a UIImageView?
- How can I open a URL in Android's web browser from my application?
.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.