Skip to content

touyu/SwiftyAttributedString

Repository files navigation

Swift 4.2.1

Features

  • Comfortable NSAttributedString
  • Better autocompletion
  • Easy range specification
  • Chainable methods
  • Customizable

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"
        .attr
        .font(.systemFont(ofSize: 30))
        .range(of: "Swifty") {
            $0.foregroundColor(.blue)
        }
        .range(of: "Attributed") {
            $0.foregroundColor(.red)
            $0.underlineStyle(.single)
        }
        .range(of: "String") {
            $0.foregroundColor(.orange)
            $0.font(.boldSystemFont(ofSize: 30))
        }
        .apply()

Documentation

All Range

"SwiftyAttributedString"
         .attr
         .font(.systemFont(ofSize: 30)) // All range
         .apply()
  

The Specific Range

"SwiftyAttributedString"
         .attr
         .range(start: 0, end: 3) {
              $0.font(.systemFont(ofSize: 30)) // The specific range
         }
         .apply()
  

Customize

"SwiftyAttributedString"
    .attr
    .customize()
    .apply()

extension SwiftyAttributedString {
    func customize() -> SwiftyAttributedString {
        return font(.systemFont(ofSize: 30))
        .range(of: "Swifty") {
            $0.foregroundColor(.blue)
        }
        .range(of: "Attributed") {
            $0.foregroundColor(.red)
            $0.underlineStyle(.single)
        }
        .range(of: "String") {
            $0.foregroundColor(.orange)
            $0.font(.boldSystemFont(ofSize: 30))
        }
    }
}