Convert Figma logo to code with AI

optonaut logoActiveLabel.swift

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

4,453
687
4,453
29

Top Related Projects

[EXPERIMENTAL] Graceful morphing effects for UILabel written in Swift.

A drop-in replacement for UILabel that supports attributes, data detectors, links, and more

A lightweight, pure-Swift library for downloading and caching images from the web.

Quick Overview

ActiveLabel.swift is a UILabel drop-in replacement that supports detecting and handling mentions, hashtags, and URLs in text. It provides customizable highlighting and tap handling for these elements, making it easy to create interactive labels in iOS applications.

Pros

  • Easy integration as a drop-in replacement for UILabel
  • Customizable appearance for mentions, hashtags, and URLs
  • Supports custom patterns for detecting additional types of elements
  • Provides tap handling for interactive elements

Cons

  • Limited to iOS platform
  • May have performance impact on large amounts of text
  • Requires manual updates to keep up with Swift language changes
  • Limited documentation and examples

Code Examples

  1. Basic usage:
let label = ActiveLabel()
label.text = "This is a #hashtag, @mention, and https://example.com"
label.numberOfLines = 0
label.enabledTypes = [.mention, .hashtag, .url]
  1. Customizing appearance:
label.hashtagColor = .blue
label.mentionColor = .red
label.URLColor = .green
label.customColor[.mention] = UIColor.purple
  1. Adding custom patterns:
label.customRegex = try! NSRegularExpression(pattern: "\\d+", options: [])
label.customColor[.custom] = UIColor.orange
  1. Handling taps:
label.handleHashtagTap { hashtag in
    print("Tapped hashtag: \(hashtag)")
}
label.handleMentionTap { mention in
    print("Tapped mention: \(mention)")
}

Getting Started

  1. Install ActiveLabel.swift using CocoaPods by adding the following to your Podfile:
pod 'ActiveLabel'
  1. Import the module in your Swift file:
import ActiveLabel
  1. Create an instance of ActiveLabel and configure it:
let label = ActiveLabel()
label.text = "Hello @world! Check out #ActiveLabel at https://github.com/optonaut/ActiveLabel.swift"
label.numberOfLines = 0
label.enabledTypes = [.mention, .hashtag, .url]
view.addSubview(label)
  1. Customize appearance and handle taps as needed using the examples provided above.

Competitor Comparisons

[EXPERIMENTAL] Graceful morphing effects for UILabel written in Swift.

Pros of LTMorphingLabel

  • Offers a variety of morphing effects for text transitions
  • Supports both Swift and Objective-C
  • Provides customizable animation duration and curve

Cons of LTMorphingLabel

  • Focused solely on text morphing, lacking broader text interaction features
  • May have higher performance overhead for complex animations
  • Less suitable for creating interactive, tappable text elements

Code Comparison

LTMorphingLabel:

let label = LTMorphingLabel()
label.text = "Hello, World!"
label.morphingEffect = .evaporate
label.morphingDuration = 0.6

ActiveLabel:

let label = ActiveLabel()
label.text = "Hello, #world! @mention"
label.enabledTypes = [.mention, .hashtag]
label.handleHashtagTap { hashtag in print("Tapped #\(hashtag)") }

Summary

LTMorphingLabel excels in creating visually appealing text transitions with various morphing effects, making it ideal for eye-catching UI elements. It offers flexibility in animation customization and language support. However, it lacks the interactive features and broader text handling capabilities of ActiveLabel.

ActiveLabel, on the other hand, focuses on creating interactive text elements with support for hashtags, mentions, and custom regex patterns. It's better suited for social media-like interfaces or any scenario requiring tappable text elements.

The choice between these libraries depends on whether the priority is visual text transitions (LTMorphingLabel) or interactive text elements (ActiveLabel).

A drop-in replacement for UILabel that supports attributes, data detectors, links, and more

Pros of TTTAttributedLabel

  • Mature and well-established library with a long history of development and community support
  • Offers more advanced text styling options, including custom link attributes and data detectors
  • Provides better support for older iOS versions

Cons of TTTAttributedLabel

  • Written in Objective-C, which may be less preferred for modern Swift-based projects
  • Requires more setup and configuration compared to ActiveLabel.swift
  • Less focused on specific social media features like hashtags and mentions

Code Comparison

TTTAttributedLabel:

TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor darkGrayColor];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;

ActiveLabel.swift:

let label = ActiveLabel()
label.numberOfLines = 0
label.enabledTypes = [.mention, .hashtag, .url]
label.urlMaximumLength = 30
label.customize { label in
    label.text = "This is a post with #hashtags and @usernames."
}

ActiveLabel.swift focuses on simplicity and ease of use for social media-style text, while TTTAttributedLabel offers more comprehensive text styling options but requires more setup. ActiveLabel.swift is written in Swift and tailored for modern iOS development, whereas TTTAttributedLabel provides broader compatibility with older iOS versions.

A lightweight, pure-Swift library for downloading and caching images from the web.

Pros of Kingfisher

  • Focuses on image downloading and caching, providing a comprehensive solution for handling images in iOS apps
  • Offers advanced features like image processing, prefetching, and placeholder support
  • Has a larger community and more frequent updates, ensuring better long-term support and compatibility

Cons of Kingfisher

  • Limited to image-related functionality, unlike ActiveLabel.swift which specializes in text handling
  • May be overkill for projects that only need basic image loading capabilities
  • Requires more setup and configuration compared to ActiveLabel.swift's simpler implementation

Code Comparison

ActiveLabel.swift:

let label = ActiveLabel()
label.enabledTypes = [.mention, .hashtag, .url]
label.text = "This is a #sample with @mentions and http://links.com"

Kingfisher:

let imageView = UIImageView()
imageView.kf.setImage(with: URL(string: "https://example.com/image.jpg"))

While both libraries serve different purposes, this comparison highlights the simplicity of use in both cases. ActiveLabel.swift focuses on creating interactive labels with custom types, while Kingfisher simplifies image loading and caching in iOS applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ActiveLabel.swift Carthage compatible Build Status

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

Features

  • Swift 5.0 (1.1.0+) and 4.2 (1.0.1)
  • Default support for Hashtags, Mentions, Links, Emails
  • 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

Install (iOS 10+)

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

label.enabledTypes = [.mention, .hashtag, .url, .email]

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.shared.openURL(url) }
handleEmailTap: (String) -> ()
label.handleEmailTap { email in print("\(email) tapped") }
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) }

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