github

eddiekaiger / SwiftyAttributes

  • среда, 12 октября 2016 г. в 03:12:54
https://github.com/eddiekaiger/SwiftyAttributes

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



SwiftyAttributes

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

Swift Version CocoaPods Compatible Platform


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

TODO

  • Add Carthage support
  • Write tests for overloaded addition operator

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.