Swiftpack.co - fabio914/swiftly-attributed-strings as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by fabio914.
fabio914/swiftly-attributed-strings 3.0.0
Harness the power of Swift syntax to swiftly create Attributed Strings (iOS)
⭐️ 0
🕓 3 years ago
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: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

3.0.0
2 years ago

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