/SwiftyAttributes

Swift extensions that make it a breeze to work with attributed strings.

Primary LanguageSwiftMIT LicenseMIT

SwiftyAttributes

Swift extensions that make it a breeze to work with attributed strings.

Swift Version Carthage compatible CocoaPods Compatible Platform Travis CI codecov.io


The original way to create an attributed string in Swift:

let attributes: [String: AnyObject] = [
    NSForegroundColorAttributeName: UIColor.blue, 
    NSUnderlineStyleAttributeName:  NSNumber(value: NSUnderlineStyle.styleSingle.rawValue)
]
let fancyString = NSAttributedString(string: "Hello World!", attributes: attributes) 

With SwiftyAttributes, you can write the same thing like this:

let fancyString = "Hello World!".withTextColor(.blue).withUnderlineStyle(.styleSingle)

You can also easily combine attributed strings using a plus sign:

let fancyString = "Hello".withFont(.systemFont(ofSize: 12)) + " World!".withFont(.systemFont(ofSize: 18))

SwiftyAttributes Has support for every attribute that can be used in iOS.

Requirements

  • iOS 9.0+

Installation

With CocoaPods

For Swift 3:

pod 'SwiftyAttributes'

For Swift 2.3:

pod 'SwiftyAttributes', '1.1'

If using Xcode 8, you may need to add this to end of your Podfile:

post_install do |installer|
    installer.pods_project.targets.each do |target| 
        target.build_configurations.each do |config| 
            config.build_settings["SWIFT_VERSION"] = "2.3"
        end
    end
end

With Carthage

For Swift 3:

github "eddiekaiger/SwiftyAttributes"

For Swift 2.3:

github "eddiekaiger/SwiftyAttributes" == 1.1.1

Support

For questions and support, please open up an issue.

License

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