### Setup ActiveLabel with Tap Handlers (Swift) Source: https://context7.com/optonaut/activelabel.swift/llms.txt Initializes an ActiveLabel instance, configures it to detect mentions, hashtags, URLs, and emails, and sets up custom tap handlers for each type. This allows for immediate user interaction with detected patterns upon initialization. ```swift 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. Visit http://example.com or email test@example.com" label.textColor = .black // Handle hashtag taps label.handleHashtagTap { hashtag in print("Tapped hashtag: \(hashtag)") // Output: "Tapped hashtag: hashtags" } // Handle mention taps label.handleMentionTap { userHandle in print("Tapped mention: \(userHandle)") // Output: "Tapped mention: userhandle" } // Handle URL taps label.handleURLTap { url in UIApplication.shared.open(url, options: [:], completionHandler: nil) // Opens: http://example.com } // Handle email taps label.handleEmailTap { email in print("Tapped email: \(email)") // Output: "Tapped email: test@example.com" } // Add to view view.addSubview(label) ``` -------------------------------- ### Handling Mentions with ActiveLabel Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Provides a code example for setting up a tap handler specifically for mentions (@userhandle) in an ActiveLabel. When a mention is tapped, a closure is executed. ```swift label.handleMentionTap { userHandle in print("(userHandle) tapped") } ``` -------------------------------- ### Basic ActiveLabel Usage with Hashtags, Mentions, URLs, and Emails Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Demonstrates the basic setup of ActiveLabel to detect and handle mentions, hashtags, URLs, and emails within the label's text. It configures the label and sets a simple text string. ```swift 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 Pattern Detection with Regex (Swift) Source: https://context7.com/optonaut/activelabel.swift/llms.txt Demonstrates how to define and enable custom text patterns using regular expressions within ActiveLabel. It includes examples for detecting prices and stock tickers, customizing their appearance, and handling taps on these custom elements. ```swift import ActiveLabel let label = ActiveLabel() // Define custom patterns let pricePattern = ActiveType.custom(pattern: "\\$[0-9]+(\\.[0-9]{2})?") let tickerPattern = ActiveType.custom(pattern: "\\$[A-Z]{2,5}\\b") // Enable custom types along with standard ones label.enabledTypes = [.mention, .hashtag, .url, pricePattern, tickerPattern] label.text = "Buy $AAPL stock at $150.25 or check #investing and @trader" // Set custom colors label.customColor[pricePattern] = .green label.customSelectedColor[pricePattern] = .darkGreen label.customColor[tickerPattern] = .orange label.customSelectedColor[tickerPattern] = .red // Handle custom type taps label.handleCustomTap(for: pricePattern) { element in print("Tapped price: \(element)") // Output: "Tapped price: $150.25" } label.handleCustomTap(for: tickerPattern) { element in print("Tapped ticker: \(element)") // Output: "Tapped ticker: $AAPL" } ``` -------------------------------- ### Filter Mentions and Hashtags in ActiveLabel Swift Source: https://context7.com/optonaut/activelabel.swift/llms.txt This example shows how to filter mentions and hashtags in ActiveLabel using custom predicates. It defines valid lists and applies filter functions to selectively enable specific mentions and hashtags, demonstrating tap handling for valid ones. ```swift import ActiveLabel let label = ActiveLabel() label.enabledTypes = [.mention, .hashtag] label.text = "Follow @john @admin @jane and check #swift #invalid #ios" // Define valid lists let validMentions = ["john", "jane", "admin"] let validHashtags = ["swift", "ios", "programming"] // Filter mentions - only valid ones become active label.filterMention { mention in let isValid = validMentions.contains(mention) print("Checking mention: (mention) - Valid: (isValid)") return isValid } // Output: Only @john, @admin, and @jane are clickable // Filter hashtags - only valid ones become active label.filterHashtag { hashtag in let isValid = validHashtags.contains(hashtag) print("Checking hashtag: (hashtag) - Valid: (isValid)") return isValid } // Output: Only #swift and #ios are clickable (#invalid is not) label.handleMentionTap { mention in print("Valid mention tapped: (mention)") } label.handleHashtagTap { hashtag in print("Valid hashtag tapped: (hashtag)") } ``` -------------------------------- ### Batched Customization with customize Block (Swift) Source: https://context7.com/optonaut/activelabel.swift/llms.txt Illustrates using the `customize` block in ActiveLabel for efficient batch updates of label properties. This method consolidates multiple configuration changes, including text content, line spacing, colors, URL trimming, and tap handlers, into a single update cycle for performance. ```swift import ActiveLabel let label = ActiveLabel() label.customize { label in // Set text content label.text = "This is a post with #multiple #hashtags and a @userhandle. Check out https://example.com" label.numberOfLines = 0 label.lineSpacing = 4 // Configure colors 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.URLSelectedColor = UIColor(red: 82.0/255, green: 190.0/255, blue: 41.0/255, alpha: 1) // Trim long URLs label.urlMaximumLength = 30 // Set up tap handlers label.handleMentionTap { mention in print("Mention tapped: \(mention)") } label.handleHashtagTap { hashtag in print("Hashtag tapped: \(hashtag)") } label.handleURLTap { url in UIApplication.shared.open(url, options: [:], completionHandler: nil) } } // All properties are applied in a single update cycle // Output for long URL: "https://example.com/very/lo..." (trimmed to 30 chars) ``` -------------------------------- ### Batched Customization of ActiveLabel Properties Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Illustrates the recommended way to customize multiple ActiveLabel properties efficiently using the `customize` block. This method prevents multiple UI refreshes by grouping all changes. ```swift 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) } } ``` -------------------------------- ### Handling URLs with ActiveLabel Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Shows the implementation for handling tapped URLs in ActiveLabel. The tap handler receives the URL object, allowing for actions like opening the URL in a browser. ```swift label.handleURLTap { url in UIApplication.shared.openURL(url) } ``` -------------------------------- ### Handling Emails with ActiveLabel Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Presents a code snippet for setting up a tap handler for email addresses detected by ActiveLabel. The handler receives the email string, enabling actions like opening an email client. ```swift label.handleEmailTap { email in print("(email) tapped") } ``` -------------------------------- ### ActiveLabel Properties Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Defines the customizable properties for ActiveLabel, including colors for mentions, hashtags, URLs, and custom types, as well as line spacing. ```APIDOC ## ActiveLabel Properties ### Description These properties allow you to customize the appearance and behavior of different text elements within an `ActiveLabel` instance. ### Properties - **mentionColor** (UIColor) - Default: `.blueColor()` - The color for mention text. - **mentionSelectedColor** (UIColor?) - The highlight color for mentions when selected. - **hashtagColor** (UIColor) - Default: `.blueColor()` - The color for hashtag text. - **hashtagSelectedColor** (UIColor?) - The highlight color for hashtags when selected. - **URLColor** (UIColor) - Default: `.blueColor()` - The color for URL text. - **URLSelectedColor** (UIColor?) - The highlight color for URLs when selected. - **customColor** ([ActiveType : UIColor]) - A dictionary to assign specific colors to custom detected types. - **customSelectedColor** ([ActiveType : UIColor]) - A dictionary to assign specific highlight colors to custom detected types when selected. - **lineSpacing** (Float?) - The spacing between lines of text. ``` -------------------------------- ### ActiveLabel Tap Handlers Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Provides methods for defining actions to be executed when specific types of text (mentions, hashtags, URLs, emails, custom types) are tapped. ```APIDOC ## ActiveLabel Tap Handlers ### Description These methods allow you to define custom actions that occur when a user interacts with specific types of rich text elements within the label. ### Methods - **handleMentionTap**: (String) -> () - **Description**: Sets a closure to be executed when a mention (e.g., @username) is tapped. - **Example**: ```swift label.handleMentionTap { userHandle in print("\(userHandle) tapped") } ``` - **handleHashtagTap**: (String) -> () - **Description**: Sets a closure to be executed when a hashtag (e.g., #topic) is tapped. - **Example**: ```swift label.handleHashtagTap { hashtag in print("\(hashtag) tapped") } ``` - **handleURLTap**: (NSURL) -> () - **Description**: Sets a closure to be executed when a URL is tapped. - **Example**: ```swift label.handleURLTap { url in UIApplication.shared.openURL(url) } ``` - **handleEmailTap**: (String) -> () - **Description**: Sets a closure to be executed when an email address is tapped. - **Example**: ```swift label.handleEmailTap { email in print("\(email) tapped") } ``` - **handleCustomTap(for type: ActiveType, handler: (String) -> ())** - **Description**: Sets a closure to be executed when a custom-defined pattern is tapped. - **Parameters**: - **type** (ActiveType) - The custom type for which to handle taps. - **handler** ((String) -> ()) - The closure to execute when the custom type is tapped. - **Example**: ```swift let customType = ActiveType.custom(pattern: "\\w+") label.handleCustomTap(for: customType) { element in print("\(element) tapped") } ``` ``` -------------------------------- ### Delegate Pattern for Centralized ActiveLabel Event Handling in Swift Source: https://context7.com/optonaut/activelabel.swift/llms.txt Implements the ActiveLabelDelegate protocol to centralize the handling of tap events for mentions, hashtags, URLs, and emails. This approach avoids individual handlers for each tapable element, promoting cleaner code. ```swift import ActiveLabel class MyViewController: UIViewController, ActiveLabelDelegate { override func viewDidLoad() { super.viewDidLoad() let label = ActiveLabel() label.delegate = self label.enabledTypes = [.mention, .hashtag, .url, .email] label.text = "Follow @user check #topic visit http://example.com or email@test.com" // No individual handlers needed - delegate handles all view.addSubview(label) } // Centralized tap handling func didSelect(_ text: String, type: ActiveType) { switch type { case .mention: print("Mention selected: (text)") // Navigate to user profile navigateToProfile(username: text) case .hashtag: print("Hashtag selected: (text)") // Show hashtag feed showHashtagFeed(tag: text) case .url: print("URL selected: (text)") // Open in Safari View Controller openInBrowser(urlString: text) case .email: print("Email selected: (text)") // Compose email composeEmail(to: text) case .custom: print("Custom type selected: (text)") default: break } } // Helper methods func navigateToProfile(username: String) { // Implementation } func showHashtagFeed(tag: String) { // Implementation } func openInBrowser(urlString: String) { // Implementation } func composeEmail(to: String) { // Implementation } } ``` -------------------------------- ### Advanced Link Attribute Configuration in ActiveLabel Swift Source: https://context7.com/optonaut/activelabel.swift/llms.txt This snippet illustrates advanced customization of link attributes in ActiveLabel using the `configureLinkAttribute` callback. It dynamically adjusts attributes like font, underline, and shadow based on link type and selection state, including handling custom patterns. ```swift import ActiveLabel let label = ActiveLabel() let boldType = ActiveType.custom(pattern: "\\sbold\\b") label.enabledTypes = [.mention, .hashtag, .url, boldType] label.text = "This is bold text with #hashtags and @mentions" // Configure custom attributes for different states label.configureLinkAttribute = { (type, attributes, isSelected) in var attrs = attributes switch type { case .mention: // Add underline to mentions when selected if isSelected { attrs[.underlineStyle] = NSUnderlineStyle.single.rawValue } case boldType: // Change font based on selection state attrs[.font] = isSelected ? UIFont.boldSystemFont(ofSize: 18) : UIFont.boldSystemFont(ofSize: 16) case .hashtag: // Add shadow when selected if isSelected { let shadow = NSShadow() shadow.shadowOffset = CGSize(width: 1, height: 1) shadow.shadowColor = UIColor.black.withAlphaComponent(0.3) attrs[.shadow] = shadow } default: break } return attrs } label.handleCustomTap(for: boldType) { element in print("Bold text tapped: (element)") } ``` -------------------------------- ### Configure URL Trimming and Taps in ActiveLabel Swift Source: https://context7.com/optonaut/activelabel.swift/llms.txt This snippet demonstrates how to configure ActiveLabel to trim long URLs for display while retaining the full URL for tap handling. It involves setting `urlMaximumLength` and handling URL taps. ```swift import ActiveLabel let label = ActiveLabel() label.enabledTypes = [.url] // Set maximum URL display length label.urlMaximumLength = 30 label.text = "Check this out: https://github.com/optonaut/ActiveLabel.swift/tree/master/ActiveLabelDemo/very/long/path" // Display shows: "Check this out: https://github.com/optonaut/A..." // But tapping passes the full original URL to the handler label.handleURLTap { url in print("Full URL: (url.absoluteString)") // Output: "Full URL: https://github.com/optonaut/ActiveLabel.swift/tree/master/ActiveLabelDemo/very/long/path" } view.addSubview(label) ``` -------------------------------- ### ActiveLabel Filtering Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Enables filtering of detected hashtags and mentions, allowing you to specify which ones should be interactive. ```APIDOC ## ActiveLabel Filtering ### Description These properties allow you to implement custom filtering logic for hashtags and mentions, determining which ones should be recognized and made interactive by the label. ### Methods - **filterHashtag**: (String) -> Bool - **Description**: Sets a closure that determines whether a detected hashtag should be processed and made interactive. Returns `true` if the hashtag should be included, `false` otherwise. - **Example**: ```swift let validHashtags = ["#swift", "#ios"] label.filterHashtag { hashtag in validHashtags.contains(hashtag) } ``` - **filterMention**: (String) -> Bool - **Description**: Sets a closure that determines whether a detected mention should be processed and made interactive. Returns `true` if the mention should be included, `false` otherwise. - **Example**: ```swift let validMentions = ["@apple", "@google"] label.filterMention { mention in validMentions.contains(mention) } ``` ``` -------------------------------- ### Custom ActiveLabel Type with Regex and Tap Handling Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Shows how to define and use custom types in ActiveLabel based on regular expressions. It assigns specific colors for the custom type and defines a tap handler for it. ```swift 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)") } ``` -------------------------------- ### Handling Hashtags with ActiveLabel Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Demonstrates how to configure a tap handler for hashtags (#tag) within an ActiveLabel. The provided closure receives the hashtag string when it's tapped. ```swift label.handleHashtagTap { hashtag in print("(hashtag) tapped") } ``` -------------------------------- ### Filtering Mentions in ActiveLabel Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Demonstrates the use of the `filterMention` property to control which mentions are tappable. A closure defines the criteria for valid mentions that should trigger a tap action. ```swift label.filterMention { mention in validMentions.contains(mention) } ``` -------------------------------- ### Filtering Hashtags in ActiveLabel Source: https://github.com/optonaut/activelabel.swift/blob/master/README.md Explains how to use the `filterHashtag` property to selectively enable taps only for specific hashtags. A closure is provided to define the filtering logic. ```swift label.filterHashtag { hashtag in validHashtags.contains(hashtag) } ``` -------------------------------- ### Dynamically Remove and Re-add ActiveLabel Tap Handlers in Swift Source: https://context7.com/optonaut/activelabel.swift/llms.txt Demonstrates how to remove specific tap handlers for ActiveLabel elements at runtime using `removeHandle(for:)` and re-enable them by setting new handlers. This allows for dynamic control over element interactivity. ```swift import ActiveLabel let label = ActiveLabel() label.enabledTypes = [.mention, .hashtag, .url] label.text = "Check @user and #topic or visit http://example.com" // Set up initial handlers label.handleMentionTap { mention in print("Mention tapped: (mention)") } label.handleHashtagTap { hashtag in print("Hashtag tapped: (hashtag)") } label.handleURLTap { url in print("URL tapped: (url)") } // Later, disable mention taps (e.g., when user is not authenticated) label.removeHandle(for: .mention) // Mentions still appear colored but don't trigger taps // Re-enable by setting a new handler label.handleMentionTap { mention in print("Mention re-enabled: (mention)") } // Remove all handlers label.removeHandle(for: .mention) label.removeHandle(for: .hashtag) label.removeHandle(for: .url) // All active types visible but not interactive ``` -------------------------------- ### Line Spacing and Typography Customization with ActiveLabel in Swift Source: https://context7.com/optonaut/activelabel.swift/llms.txt Configures advanced text layout properties for ActiveLabel, including line spacing, minimum line height, and custom fonts for highlighted elements. This snippet shows how to adjust typography and colors for both regular and active text. ```swift import ActiveLabel let label = ActiveLabel() label.numberOfLines = 0 label.enabledTypes = [.mention, .hashtag] label.customize { label in label.text = "This is a multi-line post with #hashtags and @mentions.\nIt demonstrates custom typography and spacing.\nThe text wraps naturally with custom line height." // Typography settings label.lineSpacing = 6.0 label.minimumLineHeight = 22.0 // Custom font for active elements label.highlightFontName = "HelveticaNeue-Bold" label.highlightFontSize = 16.0 // Regular text settings label.font = UIFont.systemFont(ofSize: 14) label.textColor = .darkGray // Active element colors label.hashtagColor = .blue label.mentionColor = .red label.handleHashtagTap { hashtag in print("Hashtag: (hashtag)") } label.handleMentionTap { mention in print("Mention: (mention)") } } // Result: Multi-line text with 6pt line spacing, 22pt minimum line height, // and bold highlights for hashtags and mentions view.addSubview(label) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.