Menu & Search

Supporting links in text

April 24, 2016

If you’ve encountered a use case where you need a tappable link in a label, you might have been a bit confused. See, UILabels do not natively support tappable links.

Luckily, there are at least two workarounds at your service:

1. UITextView and NSAttributedString

You might think it’s an overkill to use a UITextView instead of a UILabel. In some cases, you might be right, but depending on your needs, this might be a completely viable solution.

To make it work, you need this short snippet of code (assuming you already have a UITextView hooked up with a IBOutlet in your View Controller):

This is it! The Candy Code will open Safari and show this lovely website. 🙂

For the full working sample, click here.

The second workaround is …

2. Using TTTAttributedLabel

Like I mentioned above – it depends on your situation, but I dislike adding dependencies for just one use case. If you have to support links in multiple places in your app, by all means use this library. If you only need it in one place, seriously consider replacing your UILabel with a UITextView and focus on more important issues!

Here’s a link to the TTTAttributedLabel repo.

 

STOP LOSING TIME WITH AUTO LAYOUT!

Take part in the 5-day course with actionable tasks that will let you become a master at recognizing and solving the most common mistakes iOS developers do with Auto Layout.

Let Auto Layout become a tool you swing with your utmost confidence!

I won't send you spam, I promise. Unsubscribe at any time. Powered by ConvertKit
Hey there! You're already subscribed to my newsletter and you've hopefully gotten some useful tips and tricks when working with iOS. If you're also working with Auto Layout, make sure to check out my book called Auto Layout Fundamentals and get a 20% off for being my subscriber! https://gum.co/autolayoutfundamentals/youareawesome
Jure Zove

A lot of things but mostly a programmer who really likes fast cars. Check me out on Twitter, if you fancy.

Related article

How to properly do buttons in table view cells using Swift closures

Note: This post is an upgrade of the original post…

A practical MVVM example in Swift – Part 2 (featuring RxSwift)

Welcome to part 2 of the practical MVVM example in…

A practical MVVM example in Swift – Part 1

The good ol’ MVC pattern has been around for a…

Type your search keyword, and press enter to search