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.
- iOS 9.0+
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
github "eddiekaiger/SwiftyAttributes"
For Swift 2.3:
github "eddiekaiger/SwiftyAttributes" == 1.1.1
For questions and support, please open up an issue.
SwiftyAttributes is available under the MIT license. See the LICENSE file for more info.