Latest 1.0.2
License MIT
Platforms ios 8.0, requires ARC
Frameworks UIKit

UILabel with custom hyperlink styling, optional interaction delegate, minimal setup

What’s up

Creating a UILabel with attributed text, links appear blue and underlined, and in most cases you don’t want them to be. It’s pretty awkward and a time-kill to try and make them simply be styled the way you desire.

LinkLabel makes this all easy. Without any extra code, links appear the same colour as the rest of your text, but underlined. It’s what the default should be.

If you want to add custom styling, that’s super simple, too. Same as you would specify attributes for your attributedText, any attributes you want to be different about your links, you add to LinkLabel’s linkTextAttributes, and highlightedLinkTextAttributes.


  1. Drag contents of Source folder into your Xcode project.
  2. Add #import "UIKit/UIGestureRecognizerSubclass.h" to Bridging Header file.


Create a label using LinkLabel, instead of UILabel.

let myLabel = LinkLabel()

Setup attributed text, including NSLinkAttributeName, as normal.

let text = "This is some text, which includes a link."
let fullRange = NSMakeRange(0, (text as NSString).length)
let linkRange = (text as NSString).rangeOfString("includes a link")

let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(15), range: fullRange)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: fullRange)
attributedString.addAttribute(NSLinkAttributeName, value: NSURL(string: "")!, range: linkRange)

If you wish to customise the link appearance:

let linkTextAttributes = [
  NSUnderlineStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue),
  NSForegroundColorAttributeName: UIColor.greenColor()

let highlightedLinkTextAttributes = [
  NSUnderlineStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue),
  NSForegroundColorAttributeName: UIColor.redColor()

label.linkTextAttributes = linkTextAttributes
label.highlightedLinkTextAttributes = highlightedLinkTextAttributes

To make it easier to respond to link taps, I’ve added in an interaction delegate. Adopt LinkLabelInteractionDelegate, and then implement the delegate function:

label.interactionDelegate = self
//MARK: LinkLabelInteractionDelegate

func linkLabelDidSelectLink(linkLabel linkLabel: LinkLabel, url: NSURL) {
  print("did select link: (url)")


Created by Andrew Hart, for TwIM.


Latest podspec

    "name": "LinkLabel",
    "version": "1.0.2",
    "summary": "UILabel with custom hyperlink styling, optional interaction delegate, minimal setup",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Andrew Hart": "[email protected]"
    "source": {
        "git": "",
        "tag": "1.0.2"
    "platforms": {
        "ios": "8.0"
    "requires_arc": true,
    "source_files": "Source/*.swift",
    "frameworks": "UIKit"

Pin It on Pinterest

Share This