XUtils

Atributika

TConvert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.


Examples

Detect and style tags, provide base style for the rest of string, don’t forget about special html symbols

let redColor = UIColor(red:(0xD0 / 255.0), green: (0x02 / 255.0), blue:(0x1B / 255.0), alpha:1.0)
let a = Attrs().foregroundColor(redColor)

let font = UIFont(name: "AvenirNext-Regular", size: 24)!
let grayColor = UIColor(white: 0x66 / 255.0, alpha: 1)
let base = Attrs().font(font).foregroundColor(grayColor)

let str = "<a>&lt;a&gt;</a>tributik<a>&lt;/a&gt;</a>"
    .style(tags: ["a": a])
    .styleBase(base)
    .attributedString

Detect and style hashtags and mentions

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

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

Detect and style phone numbers

let str = "Call me (888)555-5512"
    .stylePhoneNumbers(Attrs().foregroundColor(.red))
    .attributedString

Uber String

let links = Attrs().foregroundColor(.blue)
let phoneNumbers = Attrs().backgroundColor(.yellow)
let mentions = Attrs().font(.italicSystemFont(ofSize: 12)).foregroundColor(.black)
let b = Attrs().font(.boldSystemFont(ofSize: 12))
let u = Attrs().underlineStyle(.single)

let base = Attrs().font(.systemFont(ofSize: 12)).foregroundColor(.gray)

let str = "@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"
    .style(tags: ["u": u, "b": b])
    .styleMentions(mentions)
    .styleHashtags(links)
    .styleLinks(links)
    .stylePhoneNumbers(phoneNumbers)
    .styleBase(base)
    .attributedString

AttributedLabel


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)

Requirements

Current version is compatible with:

  • Swift 5.0+
  • iOS 11.0 or later
  • tvOS 11.0 or later
  • watchOS 4.0 or later
  • macOS 10.13 or later

Note: AttributedLabel / AttributedTextView are available only on iOS

Why does Atributika have one ’t’ in its name?

Because in Belarusian/Russian we have one letter ’t’ (атрыбутыка/атрибутика). So basically it is transcription, not a real word.

Integration

Swift Package Manager

Add dependency to Package.swift file.

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

Carthage

Add github "psharanda/Atributika" to your Cartfile

CocoaPods

Atributika is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Atributika"
pod "AtributikaViews"

Manual

  1. Add Atributika to you project as a submodule using git submodule add https://github.com/psharanda/Atributika.git
  2. Open the Atributika folder & drag Atributika.xcodeproj into your project tree
  3. Add Atributika.framework to your target’s Link Binary with Libraries Build Phase
  4. Import Atributika with import Atributika and you’re ready to go

Articles

  • coming soon...