CocoaPods 0.36 adds supports for Swift and embedded frameworks. To integrate ActiveLabel into your project add the following to your Podfile:
platform :ios, '10.0'
use_frameworks!
pod 'ActiveLabel'
Usage
import ActiveLabel
let label = ActiveLabel()
label.numberOfLines = 0
label.enabledTypes = [.mention, .hashtag, .url, .email]
label.text = "This is a post with #hashtags and a @userhandle."
label.textColor = .black
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, .email, customType]
label.text = "This is a post with #hashtags and a @userhandle."
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
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.
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) }
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
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

UILabel drop-in replacement supporting Hashtags (#), Mentions (@), URLs (http://), Emails and custom regex patterns, written in Swift
Features
UILabel
drop-in replacementInstall (iOS 10+)
Carthage
Add the following to your
Cartfile
and follow these instructionsCocoaPods
CocoaPods 0.36 adds supports for Swift and embedded frameworks. To integrate ActiveLabel into your project add the following to your
Podfile
:Usage
Custom types
Enable/disable highlighting
By default, an ActiveLabel instance has the following configuration
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:
Trim long urls
You have the possiblity to set the maximum lenght a url can have;
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) -> ()
handleHashtagTap: (String) -> ()
handleURLTap: (NSURL) -> ()
handleEmailTap: (String) -> ()
handleCustomTap(for type: ActiveType, handler: (String) -> ())
filterHashtag: (String) -> Bool
filterMention: (String) -> Bool
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.