SwiftyAttributes Documentation

repository·master·Indexed 23 days ago

https://github.com/eddiekaiger/swiftyattributes

A Swift library providing a fluent API for creating and manipulating NSAttributedString objects. It supports type-safe enums, chained extensions, and the use of Swift's Range<Int> alongside NSRange for attributed string manipulation.

Tokens
1.4K
Snippets
3
Records
5
Agent score
32%

What's inside SwiftyAttributes

  1. Create attributed strings with SwiftyAttributes

    master

    SwiftyAttributes provides a fluent API to create NSAttributedString objects using several patterns:

    1. Chained extensions: Use .with[AttributeName] methods directly on a String.
    2. Attribute array: Use .withAttributes(_:) passing an array of Attribute enum cases.
    3. Initializer: Use the swiftyAttributes: parameter in the NSAttributedString initializer.
    4. Combining strings: Use the + operator to combine multiple attributed strings.
  2. Install SwiftyAttributes

    master

    You can install SwiftyAttributes using Swift Package Manager, CocoaPods, or Carthage.

    ### Swift Package Manager
    
    ```swift
    dependencies: [
        .package(url: "https://github.com/eddiekaiger/SwiftyAttributes.git", from: "5.3.0")
    ]

    With CocoaPods

    pod 'SwiftyAttributes'

    With Carthage

    github "eddiekaiger/SwiftyAttributes"

  3. Retrieve attributes from an NSAttributedString

    master

    You can retrieve a specific attribute at a given location using the swiftyAttribute(_:at:) method. This method uses the built-in NSAttributedString.Key enum to identify the attribute and returns an optional Attribute enum case.

    let attr: Attribute? = myAttributedString.swiftyAttribute(.shadow, at: 5)
  4. Manipulate attributed strings with Range and NSRange

    master

    SwiftyAttributes extends NSMutableAttributedString and NSAttributedString to support Swift's Range<Int> type alongside traditional NSRange, making it easier to work with modern Swift string indexing.

    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
    }