Install ActiveLabel.swift via CocoaPods
masterAdd the following to your Podfile. Ensure your platform is set to iOS 10.0 or higher and use_frameworks! is enabled.
platform :ios, '10.0'
use_frameworks!
pod 'ActiveLabel'repository·master·Indexed 26 days ago
https://github.com/optonaut/activelabel.swiftA UILabel drop-in replacement for iOS that automatically highlights and makes interactive hashtags, mentions, URLs, and emails. It supports custom regex-based patterns, custom colors, and tap handlers for interactive elements.
Add the following to your Podfile. Ensure your platform is set to iOS 10.0 or higher and use_frameworks! is enabled.
platform :ios, '10.0'
use_frameworks!
pod 'ActiveLabel'Add the following line to your Cartfile to integrate ActiveLabel.swift using Carthage.
github "optonaut/ActiveLabel.swift"To avoid multiple expensive textContainer refreshes, group all property updates (text, colors, tap handlers) inside a single customize { ... } block. This ensures the label only refreshes once.
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) }
}ActiveLabel is a drop-in UILabel replacement. You can enable specific types like .mention, .hashtag, .url, and .email, and provide tap handlers for them.
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")
}You can define custom highlighting patterns using ActiveType.custom(pattern:) with a regex string. You can then set custom colors and tap handlers specifically for that type.
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)")
}Set urlMaximumLength to define the maximum character count for a URL. URLs exceeding this length will be truncated with an ellipsis (e.g., https://example.com/long...).
label.urlMaximumLength = 30Methods to provide logic for filtering which hashtags or mentions are considered valid/highlighted.
filterHashtag: (String) -> Bool
filterMention: (String) -> BoolClosures used to respond to user interactions with specific highlighted elements.
handleMentionTap: (String) -> ()
handleHashtagTap: (String) -> ()
handleURLTap: (NSURL) -> ()
handleEmailTap: (String) -> ()
handleCustomTap(for type: ActiveType, handler: (String) -> ())Properties for customizing the appearance of different label types and text spacing.
mentionColor: UIColor = .blueColor()
mentionSelectedColor: UIColor?
hashtagColor: UIColor = .blueColor()
hashtagSelectedColor: UIColor?
URLColor: UIColor = .blueColor()
URLSelectedColor: UIColor?
customColor: [ActiveType : UIColor]
customSelectedColor: [ActiveType : UIColor]
lineSpacing: Float?