Skip to content

Easily build NSAttributedString by detecting and styling HTML-like tags, hashtags, mentions, RegExp or NSDataDetector patterns.

License

Notifications You must be signed in to change notification settings

Baaroon/Atributika

 
 

Repository files navigation


Build Status Platform Platform Platform Platform Language: Swift CocoaPods Carthage

Atributika is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, hashtags, any regex or even standard ios data detectors and style them with various attributes like font, color, etc.

Intro

NSAttributedString is really powerful but still a low level API which requires a lot of work to setup things. It is especially painful if string is template and real content is known only in runtime. If you are dealing with localizations it is also not easy to build NSAttributedString.

Oh wait, but you can use Atributika!

let b = Style("b").font(.boldSystemFont(ofSize: 20)).foregroundColor(.red)
let str = "Hello <b>World</b>!!!".style(tags: b)
              .styleAll(Style.font(.systemFont(ofSize: 20)))
              .attributedString
        
label.attributedText = str

Yeah, that's much better. Atributika is easy, declarative, flexible and covers all the raw edges for you.

Features

  • detect and style HTML-like markup using custom speedy parser
  • detect and style hashtags and mentions (i.e. #something and @someone)
  • detect and style regex and NSDataDetector patterns
  • style whole string or just particular ranges
  • ... and you can chain all this to parse some uber strings!
  • clean and expressive api to build styles
  • separate set of detection utils, in case you want to use just them
  • + operator to concatenate NSAttributedString with other attributed or regular strings
  • works on iOS, tvOS, watchOS, macOS

Examples

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

let font = UIFont(name: "AvenirNext-Regular", size: 24)!

let grayColor = UIColor(white: 0x66 / 255.0, alpha: 1)
let redColor = UIColor(red:(0xD0 / 255.0), green: (0x02 / 255.0), blue:(0x1B / 255.0), alpha:1.0)

let a = Style("a").foregroundColor(redColor)

let str = "<a>&lt;a&gt;</a>tributik<a>&lt;/a&gt;</a>".style(tags: a)
            .styleAll(Style.font(font).foregroundColor(grayColor))
            .attributedString

Detect and style hashtags and mentions

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

Detect and style NSDataDetector patterns

let types: NSTextCheckingResult.CheckingType = [.phoneNumber]
let str = "Call me (888)555-5512".style(textCheckingTypes: types.rawValue, style:
            Style.foregroundColor(.red))
).attributedString

Uber String

let types: NSTextCheckingResult.CheckingType = [.phoneNumber]
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 ask any details (123)456-7890
           #swift #nsattributedstring"
    .style(tags:
        Style("u").font(.boldSystemFont(ofSize: 12)).underlineStyle(.styleSingle),
        Style("b").font(.boldSystemFont(ofSize: 12))
    )
    .styleAll(Style.font(.systemFont(ofSize: 12)).foregroundColor(.gray))
    .styleMentions(Style.font(.italicSystemFont(ofSize: 12)).foregroundColor(.black))
    .styleHashtags(Style.foregroundColor(.blue))
    .style(textCheckingTypes: types.rawValue, style: Style.backgroundColor(.yellow))
    .attributedString

TTTAtributedLabel integration

let all = Style.foregroundColor(.gray).font(.systemFont(ofSize: 16))
let link = Style("a").foregroundColor(.blue)
let activeLink = Style.foregroundColor(.brown)

let label = TTTAttributedLabel(frame: CGRect())
label.activeLinkAttributes = activeLink.attributes
label.numberOfLines = 0
label.delegate = self

let aka = "If only Bradley's arm was longer. Best photo ever. #oscars <a href=\"https://pic.twitter.com/C9U5NOtGap\">pic.twitter.com/C9U5NOtGap</a>".style(tags: link)
    .styleAll(all)
    .styleHashtags(link)

aka.detections.forEach { detection in
    switch detection.type {
    case .hashtag:
        let startIndex = aka.string.index(aka.string.startIndex, offsetBy: detection.range.lowerBound + 1)
        let endIndex = aka.string.index(aka.string.startIndex, offsetBy: detection.range.lowerBound + detection.range.count - 1)

        let hashtag = (aka.string[startIndex...endIndex])

        label.addLink(to: URL(string: "https://twitter.com/hashtag/\(hashtag)"), with: NSRange(detection.range))
    case .tag(let tag):
        if tag.name == "a", let href = tag.attributes["href"] {
            label.addLink(to: URL(string: href), with: NSRange(detection.range))
        }
    default:
        break
    }
}

label.attributedText = aka.attributedString

...

// implement TTTAttributedLabelDelegate to handle clicks
extension XXX: TTTAttributedLabelDelegate {
    func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) {
        UIApplication.shared.openURL(url)
    }
}

Requirements

Current version is compatible with:

  • Swift 3.0+
  • iOS 8.0 or later
  • tvOS 9.0 or later
  • watchOS 2.0 or later
  • macOS 10.10 or later

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

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

Integration

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"

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

License

Atributika is available under the MIT license. See the LICENSE file for more info.

About

Easily build NSAttributedString by detecting and styling HTML-like tags, hashtags, mentions, RegExp or NSDataDetector patterns.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 97.3%
  • Ruby 2.7%