SwiftyAttributes:易用、强大的属性字符串工具库

1,534 阅读1分钟
原文链接: github.com

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.

Installation

With CocoaPods:

pod 'SwiftyAttributes'

Manually:

Download the zip file (or clone the project), and drag all of the swift files from the SwiftyAttributes folder into your project.

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.