ActiveLabel.swift

repository·master·Indexed 26 days ago

https://github.com/optonaut/activelabel.swift

A 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.

Tokens
1.4K
Snippets
9
Records
9
Agent score
39%

What's inside ActiveLabel.swift

  1. Optimize performance with customize(block:)

    master

    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) }
    }
  2. Basic Usage of ActiveLabel

    master

    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")
    }
  3. Configure Custom Regex Types

    master

    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)")
    }
  4. Reference: ActiveLabel Tap Handlers

    master

    Closures used to respond to user interactions with specific highlighted elements.

    handleMentionTap: (String) -> ()
    handleHashtagTap: (String) -> ()
    handleURLTap: (NSURL) -> ()
    handleEmailTap: (String) -> ()
    handleCustomTap(for type: ActiveType, handler: (String) -> ())
  5. Reference: ActiveLabel Color and Spacing Properties

    master

    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?