"Swiftly Attributed Strings" uses most of the Swift syntactic sugar to provide an easier way to instantiate NSAttributedStrings.
import SwiftlyAttributedStrings
// ...
@IBOutlet weak var label: UILabel!
// ...
label.attributedText = Underline() { Color(.blue) { "Hello, " + Font(.boldSystemFont(ofSize: 18)) { "World" } + "!" } }.attributedString
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).
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
.package(url: "https://github.com/fabio914/swiftly-attributed-strings.git", from: "3.0.0"),
Swift 5.3
Swiftly Attributed Strings
is released under the MIT
license.