Giter Site home page Giter Site logo

tribute's Introduction

Tribute Build Status

let string = NSMutableAttributedString().add("Hello ") {
    $0.font = .systemFontOfSize(20)
    $0.color = .redColor()
    $0.underline = .StyleSingle
}.add("world ") {
    $0.stroke = .Filled(width: 2)
    $0.strokeColor = .orangeColor()
}.add("of Swift "){
    $0.font = .systemFontOfSize(12)
    $0.underline = nil
    $0.URL = NSURL(string: "http://swift.org")!
}.add(UIImage(named: "swift")!)

Not bad comparing to

let string2 = NSMutableAttributedString()
string2.appendAttributedString(NSAttributedString(string: "Hello ", attributes: [
    NSFontAttributeName: UIFont.systemFontOfSize(20),
    NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
    NSForegroundColorAttributeName: UIColor.redColor()
]))
string2.appendAttributedString(NSAttributedString(string: "world ", attributes: [
    NSFontAttributeName: UIFont.systemFontOfSize(20),
    NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
    NSForegroundColorAttributeName: UIColor.redColor(),
    NSStrokeColorAttributeName: UIColor.orangeColor(),
    NSStrokeWidthAttributeName: -2
]))
string2.appendAttributedString(NSAttributedString(string: "of Swift ", attributes: [
    NSFontAttributeName: UIFont.systemFontOfSize(12),
    NSForegroundColorAttributeName: UIColor.redColor(),
    NSStrokeColorAttributeName: UIColor.orangeColor(),
    NSStrokeWidthAttributeName: -2
]))
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "swift")
string2.appendAttributedString(NSAttributedString(attachment: attachment))

Design Goals

  1. Word processor logic: appending a string should inherit last attributes.
  2. Allow for easy customization of common properties, including toggle bold or change the font size.
  3. Flatten paragraph style and attributes, no more 5 lines of code if all you wanted is to change text alignment.
  4. Replace weird attributes with more reasonable versions (for example Attribute.Stroke vs NSStrokeWidthAttributeName).
  5. Minimal overhead: produce only required attributes.
  6. Have an attributed string ready to use every time you leave the configuration block.
  7. Replace string constants with strongly typed enums where possible.

Notes

Playground

Playgrounds do not always render NSAttributesString correctly (font variations and attachments are few of the problematic I noticed).

Workaround: Use a UILabel as a live view instead: XCPlaygroundPage.currentPage.liveView = label.

Font variations

Not all fonts have both italic and bold variations

Workaround: Use obliqueness property as a poor man italic, and expansion as a bold respectively.

Closure with one statement

When the configuration closure includes only one statement, compiler gets confused

Workaround: Specify closure type explicitly like so

string.add("text") { (inout a: Attributes) in
    a.color = .redColor()
}

If you know a better way, please open a PR, I'd love to learn from it!

Roadmap

  • Objective-C compatibility
  • Moar attributes (PRs are welcome)

This is just a tribute

tribute's People

Contributors

zats avatar nadohs avatar jamesperlman avatar czars avatar

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.