Giter Club home page Giter Club logo

linklabel's Introduction

LinkLabel

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.

Install with CocoaPods

pod 'LinkLabel'

Install Manually

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

Usage

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(.font, value: UIFont.systemFont(ofSize: 15), range: fullRange)
attributedString.addAttribute(.foregroundColor, value: UIColor.black, range: fullRange)
attributedString.addAttribute(.link, value: URL(string: "https://google.com")!, range: linkRange)

If you wish to customise the link appearance:

let linkTextAttributes: [NSAttributedStringKey: AnyObject] = [
    .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int),
    .foregroundColor: UIColor.green
]

let highlightedLinkTextAttributes: [NSAttributedStringKey: AnyObject] = [
    .underlineStyle: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue as Int),
    .foregroundColor: UIColor.red
]

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)")
}

Credit

Created by Andrew Hart, for TwIM.

ProjectDent.com

@ProjectDent

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.