UILabel drop-in replacement supporting Hashtags (#), Mentions (@) and URLs (http://) written in Swift
- Swift 2+
- Support for Hashtags, Mentions and Links
- Super easy to use and lightweight
- Works as
UILabel
drop-in replacement - Well tested and documented
import ActiveLabel
let label = ActiveLabel()
label.numberOfLines = 0
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")
}
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) }
}
label.handleMentionTap { userHandle in print("\(userHandle) tapped") }
label.handleHashtagTap { hashtag in print("\(hashtag) tapped") }
label.handleURLTap { url in UIApplication.sharedApplication().openURL(url) }
label.filterHashtag { hashtag in validHashtags.contains(hashtag) }
label.filterMention { mention in validMentions.contains(mention) }
Add the following to your Cartfile
and follow these instructions
github "optonaut/ActiveLabel.swift"
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'
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