Swiftpack.co -  Package - fabio914/swiftly-attributed-strings
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
fabio914/swiftly-attributed-strings
Harness the power of Swift syntax to swiftly create Attributed Strings (iOS)
.package(url: "https://github.com/fabio914/swiftly-attributed-strings.git", from: "3.0.0")

Swiftly Attributed Strings

"Swiftly Attributed Strings" uses most of the Swift syntactic sugar to provide an easier way to instantiate NSAttributedStrings.

Example

import SwiftlyAttributedStrings
// ...
@IBOutlet weak var label: UILabel!
// ...
label.attributedText = Underline() { Color(.blue) { "Hello, " + Font(.boldSystemFont(ofSize: 18)) { "World" } + "!" } }.attributedString

How to create your own string attributes

All you need to do is subclass Node and create one init() that takes an array of StringNode and another one that takes a closure () -> StringNode. These initializers will have to set the params dictionary accordingly (access Character Attributes for more information).

Example

import UIKit
import SwiftlyAttributedStrings

class Kern: Node {
    
    init(_ kern: Float = 0, nodes: [StringNode]) {
        var params: [NSAttributedString.Key: Any] = [:]
        if kern >= 0 { params[.kern] = kern }
        super.init(params: params, nodes: nodes)
    }
    
    convenience init(_ kern: Float = 0, closure: () -> StringNode) {
        self.init(kern, nodes: [closure()])
    }
}
label.attributedText = Kern(5) { Color(.green) { "Hello, " } + Color(.blue) { "World!" } }.attributedString

How to install

Swift Package Manager

.package(url: "https://github.com/fabio914/swiftly-attributed-strings.git", from: "3.0.0"),

Requirements

Swift 5.3

License

Swiftly Attributed Strings is released under the MIT license.

GitHub

link
Stars: 0
Last commit: 5 weeks ago

Release Notes

2.0.0
1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco