Latest 5.0.0
Homepage https://github.com/eddiekaiger/SwiftyAttributes
License MIT
Platforms ios 8.0, osx 10.11, tvos 9.0, watchos 2.0
Authors

A Swifty API for attributed strings.

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


With SwiftyAttributes, you can create attributed strings like so:

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

Alternatively, use the Attribute enum:

let fancyString = "Hello World!".withAttributes([
    .backgroundColor(.magenta),
    .strokeColor(.orange),
    .strokeWidth(1),
    .baselineOffset(5.2)
])

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 available in Cocoa and Cocoa Touch.

Requirements

  • iOS 8.0+, macOS 10.11+, watchOS 2.0+, tvOS 9.0+
  • Swift 4 or 3
  • Xcode 9 or 10

Installation

With CocoaPods

pod 'SwiftyAttributes'

With Carthage

github "eddiekaiger/SwiftyAttributes"

Usage

Initializing attributed strings in SwiftyAttributes can be done several ways:

  • Using the with[Attribute] extensions:

    "Hello World".withUnderlineColor(.red).withUnderlineStyle(.styleDouble)
  • Using the Attribute enum extensions:

    "Hello World".withAttributes([.underlineColor(.red), .underlineStyle(.styleDouble)])
  • Using the Attribute enum in an initializer:
    NSAttributedString(string: "Hello World", swiftyAttributes: [.kern(5), .backgroundColor(.gray)])

You can retrieve the attribute at a specific location using an attribute name from the Attribute.Name enum:

let attr: Attribute? = myAttributedString.swiftyAttribute(.shadow, at: 5)

Several API methods are provided to use these new enums as well as Swift’s Range type instead of NSRange. Some of the method signatures include:

extension NSMutableAttributedString {
    func addAttributes(_ attributes: [Attribute], range: Range<Int>)
    func addAttributes(_ attributes: [Attribute], range: NSRange)
    func setAttributes(_ attributes: [Attribute], range: Range<Int>)
    func setAttributes(_ attributes: [Attribute], range: NSRange)
    func replaceCharacters(in range: Range<Int>, with str: String)
    func replaceCharacters(in range: Range<Int>, with attrString: NSAttributedString)
    func deleteCharacters(in range: Range<Int>)
    func removeAttribute(_ name: NSAttributedStringKey, range: Range<Int>)
}

extension NSAttributedString {
    convenience init(string str: String, swiftyAttributes: [Attribute])
    func withAttributes(_ attributes: [Attribute]) -> NSMutableAttributedString
    func withAttribute(_ attribute: Attribute) -> NSMutableAttributedString
    func attributedSubstring(from range: Range<Int>) -> NSAttributedString
    func swiftyAttribute(_ attrName: NSAttributedStringKey, at location: Int, effectiveRange range: NSRangePointer? = nil) -> Attribute?
    func swiftyAttributes(in range: Range<Int>, options: NSAttributedString.EnumerationOptions = []) -> [([Attribute], Range<Int>)]
    func enumerateSwiftyAttributes(in enumerationRange: Range<Int>, options: NSAttributedString.EnumerationOptions = [], using block: (_ attrs: [Attribute], _ range: Range<Int>, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void)
    func enumerateSwiftyAttribute(_ attrName: NSAttributedStringKey, in enumerationRange: Range<Int>, options: NSAttributedString.EnumerationOptions = [], using block: (_ value: Any?, _ range: Range<Int>, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void)
}

extension String {
    var attributedString: NSMutableAttributedString
    func withAttributes(_ attributes: [Attribute]) -> NSMutableAttributedString
    func withAttribute(_ attribute: Attribute) -> NSMutableAttributedString
}

// ... and more!

Support

For questions, support, and suggestions, please open up an issue.

License

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

Latest podspec

{
    "name": "SwiftyAttributes",
    "version": "5.0.0",
    "summary": "A Swifty API for attributed strings.",
    "description": "SwiftyAttributes provides a clean, Swifty API for dealing with NSAttributedStrings.",
    "homepage": "https://github.com/eddiekaiger/SwiftyAttributes",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Eddie Kaiger": "[email protected]"
    },
    "source": {
        "git": "https://github.com/eddiekaiger/SwiftyAttributes.git",
        "tag": "v5.0.0"
    },
    "source_files": "SwiftyAttributes/Sources/common/*.swift",
    "osx": {
        "source_files": "SwiftyAttributes/Sources/macOS/*.swift"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.11",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "pushed_with_swift_version": "4.1"
}

Pin It on Pinterest

Share This