Latest 0.5.1
Homepage https://github.com/touyu/SwiftyAttributedString
License MIT
Platforms ios 8.0, requires ARC
Authors

Swift 3.0.2
Platform
CocoaPods compatible
Carthage compatible
License

Features

  • [x] Comfortable NSAttributedString
  • [x] Better autocompletion
  • [x] Easy range specification
  • [x] Chainable methods

Installation

Carthage

Add the following line to Cartfile:

github "touyu/SwiftyAttributedString"

Create framework:

$ carthage update --platform iOS

CocoaPods

Add the following line to Podfile:

pod 'SwiftyAttributedString'

Enter the following command at the terminal:

$ pod install

Example

textView.attributedText = "SwiftyAttributedString"
            .add(attribute: Attribute(value: .font(.systemFont(ofSize: 16))))
            .add(attribute: Attribute(value: .font(.boldSystemFont(ofSize: 16)),
                                      range: .portion(of: .string("String"))))
            .add(attribute: Attribute(value: .foregroundColor(.blue),
                                      range: .portion(of: .string("Swifty"))))
            .add(attribute: Attribute(value: .foregroundColor(.red),
                                      range: .portion(of: .string("Attributed"))))
            .add(attribute: Attribute(value: .foregroundColor(.orange),
                                      range: .portion(of: .string("String"))))
            .add(attribute: Attribute(value: .underlineStyle(1.0),
                                      range: .portion(of: .string("Attributed"))))
textView.attributedText = "SwiftyAttributedString"
            .add(attributes: [Attribute(value: .font(.systemFont(ofSize: 16))),
                              Attribute(value: .font(.boldSystemFont(ofSize: 16)),
                                        range: .portion(of: .string("String"))),
                              Attribute(value: .foregroundColor(.blue),
                                        range: .portion(of: .string("Swifty"))),
                              Attribute(value: .foregroundColor(.red),
                                        range: .portion(of: .string("Attributed"))),
                              Attribute(value: .foregroundColor(.orange),
                                        range: .portion(of: .string("String"))),
                              Attribute(value: .underlineStyle(1.0),
                                        range: .portion(of: .string("Attributed")))])

Usage

// If you do not specify a range, the value applies to the full range
let attribute = Attribute(value: .font(.systemFont(ofSize: 16)))
let attribute = Attribute(value: .font(.systemFont(ofSize: 16)),
                          range: .all)
// Applied only to "Swifty"                          
let attribute = Attribute(values: [.font(.systemFont(ofSize: 16)),
                                   .foregroundColor(.blue)],
                          range: .portion(of: .string("Swifty")))

textView.attributedText = "SwiftyAttributedString".add(attribute: attribute)
textView.attributedText = "SwiftyAttributedString".add(attributes: [attribute1, attribute2, attribute3])

References

Dictionary Key Attributed Value
NSFontAttributeName .font(UIFont)
NSForegroundColorAttributeName .foregroundColor(UIColor)
NSBackgroundColorAttributeName .backgroundColor(UIColor)
NSKernAttributeName .kern(NSNumber)
NSStrikethroughStyleAttributeName .strikethroughStyle(NSNumber)
NSUnderlineStyleAttributeName .underlineStyle(NSNumber)
NSStrokeColorAttributeName .strokeColor(UIColor)
NSStrokeWidthAttributeName .strokeWidth(NSNumber)
NSShadowAttributeName .shadow(NSShadow)
NSTextEffectAttributeName .textEffect(SwiftyAttributedString.TextEffect)
NSLinkAttributeName .link(URL)
NSBaselineOffsetAttributeName .baselineOffset(NSNumber)
NSObliquenessAttributeName .obliqueness(NSNumber)
NSExpansionAttributeName .expansion(NSNumber)

Latest podspec

{
    "name": "SwiftyAttributedString",
    "version": "0.5.1",
    "summary": "The library that makes AttributedString easy to use",
    "description": "- Better autocompletion.n- Easy range specification.n- Chainable methods.",
    "homepage": "https://github.com/touyu/SwiftyAttributedString",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Yuto Akiba": "[email protected]"
    },
    "source": {
        "git": "https://github.com/touyu/SwiftyAttributedString.git",
        "tag": "0.5.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "SwiftyAttributedString/**/*.swift",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0.2"
}

Pin It on Pinterest

Share This