Atributika Documentation

repository·master·Indexed 23 days ago

https://github.com/psharanda/atributika

A declarative API for building NSAttributedString from plain strings. Atributika allows styling of HTML-like tags, hashtags, mentions, links, and phone numbers using Attrs. It includes AtributikaViews for iOS, featuring AttributedLabel for clickable and highlighted text.

Tokens
1.7K
Snippets
3
Records
4
Agent score
31%

What's inside Atributika

  1. Build NSAttributedString using Attrs and style methods

    master

    Atributika provides a declarative API to build NSAttributedString from plain strings by identifying and styling specific patterns like HTML-like tags, hashtags, mentions, links, and phone numbers.

    Basic Tag Styling

    Use .style(tags:) to apply Attrs to specific HTML-like tags.

    let b = Attrs().font(.boldSystemFont(ofSize: 20)).foregroundColor(.red)
    label.attributedText = "Hello <b>World</b>!!!".style(tags: ["b": b]).attributedString

    Styling Hashtags and Mentions

    Use .styleHashtags() and .styleMentions() to automatically detect and style #hashtag and @mention patterns.

    let str = "#Hello @World!!!"
        .styleHashtags(Attrs().font(.boldSystemFont(ofSize: 45)))
        .styleMentions(Attrs().foregroundColor(.red))
        .attributedString

    Use .styleLinks() for URLs and .stylePhoneNumbers() for telephone patterns.

    let str = "Check this website http://google.com"
        .styleLinks(Attrs().foregroundColor(.blue))
        .attributedString

    Applying a Base Style

    Use .styleBase() to provide a default style for the entire string that isn't overridden by specific tag or pattern styling.

    let base = Attrs().font(.systemFont(ofSize: 12)).foregroundColor(.gray)
    let str = "Text with <b>bold</b>".styleBase(base).attributedString
    let b = Attrs().font(.boldSystemFont(ofSize: 20)).foregroundColor(.red)        
    
    label.attributedText = "Hello <b>World</b>!!!".style(tags: ["b": b]).attributedString
  2. Install Atributika

    master

    You can integrate Atributika into your project using Swift Package Manager, Carthage, CocoaPods, or manual installation.

    Swift Package Manager

    Add the following to your Package.swift:

      dependencies: [
        .package(url: "https://github.com/psharanda/Atributika.git", .upToNextMajor(from: "5.0.0"))
      ]

    Carthage

    Add this to your Cartfile:

    git "psharanda/Atributika"

    CocoaPods

    Add these lines to your Podfile:

    pod "Atributika"
    pod "AtributikaViews"

    Manual Installation

    1. Add Atributika as a submodule: git submodule add https://github.com/psharanda/Atributika.git
    2. Drag Atributika.xcodeproj into your project tree.
    3. Add Atributika.framework to your target's Link Binary with Libraries Build Phase.
    4. Use import Atributika in your code.
  3. Use AttributedLabel for clickable and highlighted text

    master

    AttributedLabel (part of the AtributikaViews library, available on iOS) is a UILabel subclass designed to display styled text with interactive elements like clickable links.

    Configuration

    • numberOfLines: Set to 0 to allow multi-line text.
    • highlightedLinkAttributes: Set the Attrs to use when a link is being interacted with.
    • onLinkTouchUpInside: A closure triggered when a link is tapped. The value passed is typically the link's destination (e.g., a URL string).

    You can use TagTuner or DetectionTuner to intercept detected patterns and define custom link destinations or styles.

    • TagTuner: Used for HTML-like tags (e.g., <a href="...">).
    • DetectionTuner: Used for automatically detected patterns like hashtags, mentions, or URLs.
    let tweetLabel = AttributedLabel()
    tweetLabel.numberOfLines = 0
    tweetLabel.highlightedLinkAttributes = Attrs().foregroundColor(.red).attributes
    
    // Example: Customizing a hashtag to act as a Twitter search link
    let hashtag = DetectionTuner { 
        Attrs().akaLink("https://twitter.com/hashtag/\($0.text.replacingOccurrences(of: "#", with: ""))")
    }
    
    let tweet = "#swift"
    tweetLabel.attributedText = tweet
        .styleHashtags(hashtag)
        .attributedString
    
    tweetLabel.onLinkTouchUpInside = { _, val in
        if let linkStr = val as? String, let url = URL(string: linkStr) {
            UIApplication.shared.openURL(url)
        }
    }
    let tweetLabel = AttributedLabel()
    
    tweetLabel.numberOfLines = 0
    tweetLabel.highlightedLinkAttributes = Attrs().foregroundColor(.red).attributes
    
    let baseLinkAttrs = Attrs().foregroundColor(.blue)
    
    let a = TagTuner { 
        Attrs(baseLinkAttrs).akaLink($0.tag.attributes["href"] ?? "")
    }
    
    let hashtag = DetectionTuner { 
        Attrs(baseLinkAttrs).akaLink("https://twitter.com/hashtag/\($0.text.replacingOccurrences(of: "#", with: ""))")
    }
    
    let mention = DetectionTuner { 
        Attrs(baseLinkAttrs).akaLink("https://twitter.com/\($0.text.replacingOccurrences(of: "@", with: ""))")
    }
    
    let link = DetectionTuner { 
        Attrs(baseLinkAttrs).akaLink($0.text)
    }
    
    let tweet = "@all I found <u>really</u> nice framework to manage attributed strings. It is called <b>Atributika</b>. Call me if you want to know more (123)456-7890 #swift #nsattributedstring https://github.com/psharanda/Atributika"
    
    tweetLabel.attributedText = tweet
        .style(tags: ["a": a])
        .styleHashtags(hashtag)
        .styleMentions(mention)
        .styleLinks(link)
        .attributedString
    
    tweetLabel.onLinkTouchUpInside = { _, val in
        if let linkStr = val as? String {
            if let url = URL(string: linkStr) {
                UIApplication.shared.openURL(url)
            }
        }
    }
    
    view.addSubview(tweetLabel)
  4. System Requirements for Atributika

    master

    Atributika is compatible with the following environments:

    • Swift: 5.0+
    • iOS: 11.0 or later (Note: AttributedLabel and AttributedTextView are iOS only)
    • tvOS: 11.0 or later
    • watchOS: 4.0 or later
    • macOS: 10.13 or later