optonaut/ActiveLabel.swift

Name: ActiveLabel.swift

Owner: Optonaut

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

Created: 2015-09-03 17:18:12.0

Updated: 2018-01-19 21:41:11.0

Pushed: 2018-01-15 12:50:35.0

Homepage:

Size: 1399

Language: Swift

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ActiveLabel.swift Carthage compatible Build Status

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

Features

Usage
rt ActiveLabel

label = ActiveLabel()

l.numberOfLines = 0
l.enabledTypes = [.mention, .hashtag, .url]
l.text = "This is a post with #hashtags and a @userhandle."
l.textColor = .black
l.handleHashtagTap { hashtag in
int("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.purple
label.customSelectedColor[customType] = UIColor.green

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) -> ()
l.handleMentionTap { userHandle in print("\(userHandle) tapped") }
handleHashtagTap: (String) -> ()
l.handleHashtagTap { hashtag in print("\(hashtag) tapped") }
handleURLTap: (NSURL) -> ()
l.handleURLTap { url in UIApplication.shared.openURL(url) }
handleCustomTap(for type: ActiveType, handler: (String) -> ())
l.handleCustomTap(for: customType) { element in print("\(element) tapped") }
filterHashtag: (String) -> Bool
l.filterHashtag { hashtag in validHashtags.contains(hashtag) }
filterMention: (String) -> Bool
l.filterMention { mention in validMentions.contains(mention) }
Install (iOS 8+)
Carthage

Add the following to your Cartfile and follow these instructions

ub "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:

form :ios, '8.0'
frameworks!

'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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.