/AttributedStringBuilder

Implementing NSAttributedString Swift function builder https://www.vadimbulavin.com/swift-function-builders-swiftui-view-builder/

Primary LanguageSwiftThe UnlicenseUnlicense

Article related to this project


AttributedStringBuilder

A sample project showing how to implement custom Swift function builder using NSAttributedString as an example.

Usage:

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        label.attributedText = NSAttributedString {
            NSAttributedString {
                "Folder "
                UIImage(systemName: "folder")!
            }
            
            "\n"
            
            NSAttributedString {
                "Document "
                UIImage(systemName: "doc")!.withRenderingMode(.alwaysTemplate)
            }
            .withAttributes([
                .font: UIFont.systemFont(ofSize: 32),
                .foregroundColor: UIColor.red
            ])
        }
    }
}

Result:

Swift function builders, SwiftUI ViewBuilder