Git Product home page Git Product logo

activelabel.swift's Introduction

ActiveLabel.swift Carthage compatible Build Status

UILabel drop-in replacement supporting Hashtags (#), Mentions (@), URLs (http://) and custom regex patterns, written in Swift

Features

  • Swift 2+
  • Default support for Hashtags, Mentions, Links
  • Support for custom types via regex
  • Ability to enable highlighting only for the desired types
  • Ability to trim urls
  • Super easy to use and lightweight
  • Works as UILabel drop-in replacement
  • Well tested and documented

Usage

import ActiveLabel

let label = ActiveLabel()

label.numberOfLines = 0
label.enabledTypes = [.Mention, .Hashtag, .URL]
label.text = "This is a post with #hashtags and a @userhandle."
label.textColor = .blackColor()
label.handleHashtagTap { hashtag in
  print("Success. You just tapped the \(hashtag) hashtag")
}

Custom types

    let customType = ActiveType.Custom(pattern: "\\swith\\b") //Regex that looks for "with"
    label.enabledTypes = [.Mention, .Hashtag, .URL, customType]
    
    label.customColor[customType] = UIColor.purpleColor()
    label.customSelectedColor[customType] = UIColor.greenColor()
    
    label.handleCustomTap(for: customType) { element in 
        print("Custom type tapped: \(element)") 
    }

Enable/disable highlighting

By default, an ActiveLabel instance has the following configuration

    label.enabledTypes = [.Mention, .Hashtag, .URL]

But feel free to enable/disable to fit your requirements

Batched customization

When using ActiveLabel, it is recommended to use the customize(block:) method to customize it. The reason is that ActiveLabel is reacting to each property that you set. So if you set 3 properties, the textContainer is refreshed 3 times.

When using customize(block:), you can group all the customizations on the label, that way ActiveLabel is only going to refresh the textContainer once.

Example:

        label.customize { label in
            label.text = "This is a post with #multiple #hashtags and a @userhandle."
            label.textColor = UIColor(red: 102.0/255, green: 117.0/255, blue: 127.0/255, alpha: 1)
            label.hashtagColor = UIColor(red: 85.0/255, green: 172.0/255, blue: 238.0/255, alpha: 1)
            label.mentionColor = UIColor(red: 238.0/255, green: 85.0/255, blue: 96.0/255, alpha: 1)
            label.URLColor = UIColor(red: 85.0/255, green: 238.0/255, blue: 151.0/255, alpha: 1)
            label.handleMentionTap { self.alert("Mention", message: $0) }
            label.handleHashtagTap { self.alert("Hashtag", message: $0) }
            label.handleURLTap { self.alert("URL", message: $0.absoluteString) }
        }

Trim long urls

You have the possiblity to set the maximum lenght a url can have;

        label.urlMaximumLength = 30

From now on, a url that's bigger than that, will be trimmed.

https://afancyurl.com/whatever -> https://afancyurl.com/wh...

API

mentionColor: UIColor = .blueColor()
mentionSelectedColor: UIColor?
hashtagColor: UIColor = .blueColor()
hashtagSelectedColor: UIColor?
URLColor: UIColor = .blueColor()
URLSelectedColor: UIColor?

customColor: [ActiveType : UIColor]

customSelectedColor: [ActiveType : UIColor]

lineSpacing: Float?
handleMentionTap: (String) -> ()
label.handleMentionTap { userHandle in print("\(userHandle) tapped") }
handleHashtagTap: (String) -> ()
label.handleHashtagTap { hashtag in print("\(hashtag) tapped") }
handleURLTap: (NSURL) -> ()
label.handleURLTap { url in UIApplication.sharedApplication().openURL(url) }
handleCustomTap(for type: ActiveType, handler: (String) -> ())
label.handleCustomTap(for: customType) { element in print("\(element) tapped") }
filterHashtag: (String) -> Bool
label.filterHashtag { hashtag in validHashtags.contains(hashtag) }
filterMention: (String) -> Bool
label.filterMention { mention in validMentions.contains(mention) }

Install (iOS 8+)

Carthage

Add the following to your Cartfile and follow these instructions

github "optonaut/ActiveLabel.swift"

CocoaPods

CocoaPods 0.36 adds supports for Swift and embedded frameworks. To integrate ActiveLabel into your project add the following to your Podfile:

platform :ios, '8.0'
use_frameworks!

pod 'ActiveLabel'

Alternatives

Before writing ActiveLabel we've tried a lot of the following alternatives but weren't quite satisfied with the quality level or ease of usage, so we decided to contribute our own solution.

  • TTTAttributedLabel (ObjC) - A drop-in replacement for UILabel that supports attributes, data detectors, links, and more
  • STTweetLabel (ObjC) - A UILabel with #hashtag @handle and links tappable
  • AMAttributedHighlightLabel (ObjC) - A UILabel subclass with mention/hashtag/link highlighting
  • KILabel (ObjC) - A simple to use drop in replacement for UILabel for iOS 7 and above that highlights links such as URLs, twitter style usernames and hashtags and makes them touchable

activelabel.swift's People

Contributors

bampt avatar econa77 avatar efremidze avatar kaandedeoglu avatar krjackso avatar lastmove avatar paullafytskyi avatar polqf avatar schickling avatar tony-pizza avatar wowbroforce avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.