Giter Site home page Giter Site logo

attributed's Introduction

Attributed

Build Status CodeCov Swift 4.2 CocoaPods compatible CocoaPods Carthage compatible License

µframework for Attributed strings.

What is Attributed?

Attributed aims to be a drop in replacement to the current verstion of the NSAttributedString API. The existing interface to using attributed strings has a few flaws, namely if you dont know the Key and type of value needed to set a certain attribute, you have spend time checking documentation or the reading the comments for NSAttributedString. Another concern is safety, passing a dictionary of type [String: Any] to the constructor of NSAttributedString is a potential crash at runtime waiting to happen.

By wrapping the current official interface to NSAttributedString into a fluent easy to use API, Attributed was made in an attempt to give developers an alternative option to the official interface.

Features

  • Create NSAttributedString instances with a simple and fluid interface
  • Combine NSAttributedStrings with +
  • Partially apply Attributes to parts of an NSAttributedString by providing a Range

Dont see a feature you need?

Feel free to open an Issue requesting the feature you want or send over a pull request!

Usage

Creating a new NSAttributedString by closure composition

"This is not a string".at.attributed {
  return $0.foreground(color: .red)
           .font(UIFont(name: "Chalkduster", size: 24.0)!)
           .underlineStyle(.styleSingle)
}

Creating a new NSAttributedString by passing an attributes object

First create an Attributes object:

let attributes = Attributes {
    return $0.foreground(color: .red)
             .font(UIFont(name: "Chalkduster", size: 24.0)!)
             .underlineStyle(.styleSingle)
}

then simply apply the Attributes to a String:

"Hello".at.attributed(with: attributes)

Combining NSAttributedString with +

This library defines an concatenation operator + for concatentating instances of NSAttributedString. + works with NSAttributedString no different than it does for String. This can be useful for combining NSAttributedStrings with different attributes to produce the desired effect without having to specify ranges to apply different attributes to.

let bodyAttributes = Attributes { 
    return $0.foreground(color: .purple)
             .font(UIFont(name: "Noteworthy-Light", size: 20.0)!)
}

let authorAttributes = bodyAttributes.foreground(color: .black)

"I think theres something strangely musical about noise.".at.attributed(with: bodyAttributes)
+ "\n  - Trent Reznor".at.attributed(with: authorAttributes)

Installation

Carthage

If you use Carthage to manage your dependencies, simply add Attributed to your Cartfile:

github "Nirma/Attributed"

If you use Carthage to build your dependencies, make sure you have added Attributed.framework to the "Linked Frameworks and Libraries" section of your target, and have included Attributed.framework in your Carthage framework copying build phase.

CocoaPods

If you use CocoaPods to manage your dependencies, simply add Attributed to your Podfile:

pod 'AttributedLib'

Requirements

  • Xcode 9.0
  • Swift 4.0+

Contribution

Contributions are more than welcome!

There are currently three main branches and their use is described as follows:

Branch Purpose
master The latest stable release.
development Next release development branch. Not safe to point to directly
attributed-3.0 The next major release of Attributed, release goal is currently set for July 1, 2018.

License

Attributed is free software, and may be redistributed under the terms specified in the LICENSE file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.